test

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

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 断言为假

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 断言为真

func SetupRouter

func SetupRouter() *gin.Engine

SetupRouter 创建测试路由

Types

type MockCache

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

MockCache 模拟缓存

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 设置缓存

type MockDB

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

MockDB 模拟数据库(用于简单测试)

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 模拟存储

func NewMockStorage

func NewMockStorage() *MockStorage

NewMockStorage 创建模拟存储

func (*MockStorage) GetURL

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

GetURL 获取 URL

func (*MockStorage) Upload

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

Upload 模拟上传

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() *httptest.ResponseRecorder

Execute 执行请求

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 请求体

func (*Request) WithToken

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

WithToken 设置 Authorization 头

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 包含指定字段

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 断言状态码

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