Home >Backend Development >Golang >Mocks and stubs in Golang function testing
In Go function testing, Mocks and Stubs allow isolation and verification of external dependencies. Mocks simulate the behavior of dependencies, while Stubs provide fixed values. To use Mocks, you need to use mockgen to generate mock files, introduce mocks in tests, and set mock expectations. To use Stubs, you need to introduce the stub package, create the stub, and assert whether the stub is called. By using Mocks and Stubs, we can improve the robustness and confidence of function testing.
How to use Mocks and Stubs in Go function testing
When performing function testing in Go, Mocks and Stubs are isolated and effective techniques for validating external dependencies. Below we'll explore how to use these techniques in testing.
What are Mocks and Stubs?
Mock is an alternative to mocking external dependencies, allowing you to specify their behavior and characteristics in your tests. In contrast, a stub is a replacement that provides fixed values without simulating the behavior of dependencies.
Use Mocks:
Use mockgen
to generate the mock
file:
go get github.com/golang/mock/mockgen mockgen -package=api -destination=mocks/customer_mock.go github.com/your-org/your-repo/api CustomerService
in the test function Introducing Mock:
import mocks "github.com/your-org/your-repo/api/mocks" func TestFunctionWithCustomerService(t *testing.T) { mockCustomerService := &mocks.CustomerService{} }
Set Mock expected value:
// 设置客户创建方法的预期行为 mockCustomerService.On("CreateCustomer", mock.Anything).Return(nil, nil)
Use Stubs:
Introduce Stub package:
import "github.com/stretchr/testify/stub"
In the test function Create Stub in:
// 创建一个提供固定数字值的 `Stub`,作为 avg 升序的占位符 stubAvg := stub.On(ioutil, "ReadFile").Return([]byte("80"), nil)
Practical case:
Suppose we have a function GetCustomerAge
, which depends on dependency CustomerService
Get customer data.
func GetCustomerAge(customerID int64) (int, error) { customer, err := service.GetCustomer(customerID) if err != nil { return 0, err } return customer.Age, nil }
Use Mock to test:
func TestGetCustomerAgeWithMock(t *testing.T) { mockCustomerService := &mocks.CustomerService{} // 设置 Mock 期望值 mockCustomerService.On("GetCustomer", mock.Anything).Return(&models.Customer{Age: 25}, nil) age, err := GetCustomerAge(1) assert.NoError(t, err) assert.Equal(t, 25, age) }
Use Stub to test:
func TestGetCustomerAgeWithStub(t *testing.T) { stubCustomerService := stub.On(service, "GetCustomer").Return(&models.Customer{Age: 25}, nil) age, err := GetCustomerAge(1) assert.NoError(t, err) assert.Equal(t, 25, age) // 断言 Stub 是否被调用 assert.True(t, stubCustomerService.Called()) }
By using Mock and Stub, we can Isolate functions from external dependencies and verify their behavior in real-world environments, increasing the robustness and confidence of function testing.
The above is the detailed content of Mocks and stubs in Golang function testing. For more information, please follow other related articles on the PHP Chinese website!