让我们看一个全面的示例,其中涵盖了stretchr/testify库的常见功能以及golang中的mockery。此示例将包括使用断言进行测试、使用 require 包进行严格断言、测试 http 处理程序以及使用 mockery 模拟依赖项。
假设我们有一个从外部 api 获取用户信息的服务。我们想要测试:
/project │ ├── main.go ├── service.go ├── service_test.go ├── user_client.go ├── mocks/ │ └── userclient.go (generated by mockery) └── go.mod
user_client.go
该文件定义了与外部用户 api 交互的接口。
package project type user struct { id int name string } type userclient interface { getuserbyid(id int) (*user, error) }
service.go
该文件包含一个使用 userclient 获取用户详细信息的服务。
package project import "fmt" type userservice struct { client userclient } func newuserservice(client userclient) *userservice { return &userservice{client: client} } func (s *userservice) getuserdetails(id int) (string, error) { user, err := s.client.getuserbyid(id) if err != nil { return "", fmt.errorf("failed to get user: %w", err) } return fmt.sprintf("user: %s (id: %d)", user.name, user.id), nil }
使用嘲讽生成模拟
您可以使用 mockery 为 userclient 生成模拟:
mockery --name=userclient --output=./mocks
这将在mocks/userclient.go 中生成一个模拟。
service_test.go
现在,让我们使用 testify 断言和模拟生成的模拟为 userservice 编写一个测试。
package project_test import ( "errors" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/mock" "project" "project/mocks" ) func TestUserService_GetUserDetails_Success(t *testing.T) { // Create a new mock client mockClient := new(mocks.UserClient) // Define what the mock should return when `GetUserByID` is called mockClient.On("GetUserByID", 1).Return(&project.User{ ID: 1, Name: "John Doe", }, nil) // Create the UserService with the mock client service := project.NewUserService(mockClient) // Test the GetUserDetails method result, err := service.GetUserDetails(1) // Use `require` for error checks require.NoError(t, err) require.NotEmpty(t, result) // Use `assert` for value checks assert.Equal(t, "User: John Doe (ID: 1)", result) // Ensure that the `GetUserByID` method was called exactly once mockClient.AssertExpectations(t) } func TestUserService_GetUserDetails_Error(t *testing.T) { // Create a new mock client mockClient := new(mocks.UserClient) // Define what the mock should return when `GetUserByID` is called with an error mockClient.On("GetUserByID", 2).Return(nil, errors.New("user not found")) // Create the UserService with the mock client service := project.NewUserService(mockClient) // Test the GetUserDetails method result, err := service.GetUserDetails(2) // Use `require` for error checks require.Error(t, err) assert.Contains(t, err.Error(), "user not found") // Ensure that the result is empty assert.Empty(t, result) // Ensure that the `GetUserByID` method was called exactly once mockClient.AssertExpectations(t) }
此设置涵盖了stretcher/testify 的断言和模拟的基本功能,为 golang 中的单元测试提供了结构化且可维护的方法。
以上就是使用 STRETCHR/TESTIFY 和 MOCKERY 进行 GOL 测试的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号