test

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMockStorageTooLarge = errors.New("mock storage: 文件超过大小限制")

ErrMockStorageTooLarge 表示 mock storage 收到超过内存保护上限的内容。

Functions

func AssertEqual

func AssertEqual(t *testing.T, expected, actual any)

AssertEqual 断言相等。

func AssertError

func AssertError(t *testing.T, err error)

AssertError 断言有错误。

func AssertFalse

func AssertFalse(t *testing.T, value bool)

AssertFalse 断言为 false。

func AssertNil

func AssertNil(t *testing.T, value any)

AssertNil 断言为空。

func AssertNoError

func AssertNoError(t *testing.T, err error)

AssertNoError 断言无错误。

func AssertNotNil

func AssertNotNil(t *testing.T, value any)

AssertNotNil 断言非空。

func AssertTrue

func AssertTrue(t *testing.T, value bool)

AssertTrue 断言为 true。

func SetupRouter

func SetupRouter() *gin.Engine

SetupRouter 创建不带框架默认中间件的测试路由。

需要 RequestID、Recover、超时等框架中间件的集成测试,应直接构造 xlgo.App 或手动 Use 对应中间件。

Types

type MockCache

type MockCache struct {
	// contains filtered or unexported fields
}

MockCache 是并发安全的简单内存缓存 mock。

func NewMockCache

func NewMockCache() *MockCache

NewMockCache 创建模拟缓存。

func (*MockCache) Clear

func (m *MockCache) Clear()

Clear 清空缓存。

func (*MockCache) Delete

func (m *MockCache) Delete(key string)

Delete 删除缓存。

func (*MockCache) Exists

func (m *MockCache) Exists(key string) bool

Exists 检查缓存是否存在。

func (*MockCache) Get

func (m *MockCache) Get(key string) ([]byte, bool)

Get 获取缓存,返回值会复制一份,避免调用方修改内部状态。

func (*MockCache) Set

func (m *MockCache) Set(key string, value []byte)

Set 设置缓存,内部会复制 value,避免调用方后续修改污染 mock。

type MockDB

type MockDB struct {
	// contains filtered or unexported fields
}

MockDB 是并发安全的简单内存数据库 mock。

func NewMockDB

func NewMockDB() *MockDB

NewMockDB 创建模拟数据库。

func (*MockDB) Clear

func (m *MockDB) Clear()

Clear 清空数据。

func (*MockDB) Delete

func (m *MockDB) Delete(key any)

Delete 删除数据。

func (*MockDB) Get

func (m *MockDB) Get(key any) (any, bool)

Get 获取数据。

func (*MockDB) Set

func (m *MockDB) Set(key, value any)

Set 设置数据。

type MockStorage

type MockStorage struct {
	// contains filtered or unexported fields
}

MockStorage 是并发安全的简单内存存储 mock。

func NewMockStorage

func NewMockStorage() *MockStorage

NewMockStorage 创建模拟存储。

func (*MockStorage) GetURL

func (m *MockStorage) GetURL(path string) string

GetURL 获取 mock URL。

func (*MockStorage) Upload

func (m *MockStorage) Upload(file *multipart.FileHeader, subdir string) (string, error)

Upload 模拟上传,签名对齐 storage.Upload。

func (*MockStorage) UploadFromBytes added in v1.2.0

func (m *MockStorage) UploadFromBytes(data []byte, filename, subdir string) (string, error)

UploadFromBytes 模拟字节上传,签名对齐 storage.UploadFromBytes。

type Request

type Request struct {
	// contains filtered or unexported fields
}

Request 是测试请求构造器。

func DELETE

func DELETE(router *gin.Engine, path string) *Request

DELETE 创建 DELETE 请求。

func GET

func GET(router *gin.Engine, path string) *Request

GET 创建 GET 请求。

func NewRequest

func NewRequest(router *gin.Engine, method, path string) *Request

NewRequest 创建测试请求。

func PATCH

func PATCH(router *gin.Engine, path string) *Request

PATCH 创建 PATCH 请求。

func POST

func POST(router *gin.Engine, path string) *Request

POST 创建 POST 请求。

func PUT

func PUT(router *gin.Engine, path string) *Request

PUT 创建 PUT 请求。

func (*Request) Execute

func (r *Request) Execute() *Response

Execute 执行请求并返回带断言方法的响应包装。

func (*Request) ExecuteRecorder added in v1.3.0

func (r *Request) ExecuteRecorder() *httptest.ResponseRecorder

ExecuteRecorder 执行请求并返回原始 httptest.ResponseRecorder。

func (*Request) WithBody

func (r *Request) WithBody(body any) *Request

WithBody 设置请求体。

func (*Request) WithHeader

func (r *Request) WithHeader(key, value string) *Request

WithHeader 设置请求头。

func (*Request) WithJSON

func (r *Request) WithJSON(body any) *Request

WithJSON 设置 JSON 请求体并写入 Content-Type。

func (*Request) WithToken

func (r *Request) WithToken(token string) *Request

WithToken 设置 Authorization Bearer token。

type Response

type Response struct {
	*httptest.ResponseRecorder
}

Response 是测试响应辅助包装。

func (*Response) AssertBadRequest

func (r *Response) AssertBadRequest(t *testing.T)

AssertBadRequest 断言状态码为 400。

func (*Response) AssertCreated

func (r *Response) AssertCreated(t *testing.T)

AssertCreated 断言状态码为 201。

func (*Response) AssertForbidden

func (r *Response) AssertForbidden(t *testing.T)

AssertForbidden 断言状态码为 403。

func (*Response) AssertJSONContains

func (r *Response) AssertJSONContains(t *testing.T, key string, expected any)

AssertJSONContains 断言 JSON 响应包含指定字段和值,支持 data.user.id 形式的嵌套键。

func (*Response) AssertNotFound

func (r *Response) AssertNotFound(t *testing.T)

AssertNotFound 断言状态码为 404。

func (*Response) AssertOK

func (r *Response) AssertOK(t *testing.T)

AssertOK 断言状态码为 200。

func (*Response) AssertStatus

func (r *Response) AssertStatus(t *testing.T, expected int)

AssertStatus 断言 HTTP 状态码。

func (*Response) AssertUnauthorized

func (r *Response) AssertUnauthorized(t *testing.T)

AssertUnauthorized 断言状态码为 401。

func (*Response) ParseJSON

func (r *Response) ParseJSON(v any) error

ParseJSON 解析 JSON 响应。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL