Documentation
¶
Index ¶
- Variables
- func AssertEqual(t *testing.T, expected, actual any)
- func AssertError(t *testing.T, err error)
- func AssertFalse(t *testing.T, value bool)
- func AssertNil(t *testing.T, value any)
- func AssertNoError(t *testing.T, err error)
- func AssertNotNil(t *testing.T, value any)
- func AssertTrue(t *testing.T, value bool)
- func SetupRouter() *gin.Engine
- type MockCache
- type MockDB
- type MockStorage
- type Request
- func DELETE(router *gin.Engine, path string) *Request
- func GET(router *gin.Engine, path string) *Request
- func NewRequest(router *gin.Engine, method, path string) *Request
- func PATCH(router *gin.Engine, path string) *Request
- func POST(router *gin.Engine, path string) *Request
- func PUT(router *gin.Engine, path string) *Request
- func (r *Request) Execute() *Response
- func (r *Request) ExecuteRecorder() *httptest.ResponseRecorder
- func (r *Request) WithBody(body any) *Request
- func (r *Request) WithHeader(key, value string) *Request
- func (r *Request) WithJSON(body any) *Request
- func (r *Request) WithToken(token string) *Request
- type Response
- func (r *Response) AssertBadRequest(t *testing.T)
- func (r *Response) AssertCreated(t *testing.T)
- func (r *Response) AssertForbidden(t *testing.T)
- func (r *Response) AssertJSONContains(t *testing.T, key string, expected any)
- func (r *Response) AssertNotFound(t *testing.T)
- func (r *Response) AssertOK(t *testing.T)
- func (r *Response) AssertStatus(t *testing.T, expected int)
- func (r *Response) AssertUnauthorized(t *testing.T)
- func (r *Response) ParseJSON(v any) error
Constants ¶
This section is empty.
Variables ¶
var ErrMockStorageTooLarge = errors.New("mock storage: 文件超过大小限制")
ErrMockStorageTooLarge 表示 mock storage 收到超过内存保护上限的内容。
Functions ¶
func SetupRouter ¶
SetupRouter 创建不带框架默认中间件的测试路由。
需要 RequestID、Recover、超时等框架中间件的集成测试,应直接构造 xlgo.App 或手动 Use 对应中间件。
Types ¶
type MockCache ¶
type MockCache struct {
// contains filtered or unexported fields
}
MockCache 是并发安全的简单内存缓存 mock。
type MockDB ¶
type MockDB struct {
// contains filtered or unexported fields
}
MockDB 是并发安全的简单内存数据库 mock。
type MockStorage ¶
type MockStorage struct {
// contains filtered or unexported fields
}
MockStorage 是并发安全的简单内存存储 mock。
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 NewRequest ¶
NewRequest 创建测试请求。
func (*Request) ExecuteRecorder ¶ added in v1.3.0
func (r *Request) ExecuteRecorder() *httptest.ResponseRecorder
ExecuteRecorder 执行请求并返回原始 httptest.ResponseRecorder。
func (*Request) WithHeader ¶
WithHeader 设置请求头。
type Response ¶
type Response struct {
*httptest.ResponseRecorder
}
Response 是测试响应辅助包装。
func (*Response) AssertBadRequest ¶
AssertBadRequest 断言状态码为 400。
func (*Response) AssertCreated ¶
AssertCreated 断言状态码为 201。
func (*Response) AssertForbidden ¶
AssertForbidden 断言状态码为 403。
func (*Response) AssertJSONContains ¶
AssertJSONContains 断言 JSON 响应包含指定字段和值,支持 data.user.id 形式的嵌套键。
func (*Response) AssertNotFound ¶
AssertNotFound 断言状态码为 404。
func (*Response) AssertStatus ¶
AssertStatus 断言 HTTP 状态码。
func (*Response) AssertUnauthorized ¶
AssertUnauthorized 断言状态码为 401。