Mocking techniques in Go function unit testing

WBOY
Release: 2024-04-30 18:21:01
Original
612 people have browsed it

Mocking in unit testing is a technique of creating test doubles in unit tests to replace external dependencies, allowing specific functions to be isolated and tested. The basic principles are: define interface, create mock, inject mock. To use GoogleMock mocking, you need to define the interface, create the mock, and inject it in the test function. To use testify/mock simulation, you need to declare the MockClient structure, set the expected value for the Get method, and set the simulation in the test function.

Go 函数单元测试中的模拟技巧

Mocking techniques in Go function unit testing

In unit testing, mocking is a technique for creating test doubles to replace External dependencies in the code under test. This allows you to isolate and test specific functions without interacting with other components.

Basic principles of simulation

The basic principles of simulation are:

  1. Define the interface: Create an interface that represents the component to be simulated.
  2. Create a mock: Create a mock implementation of this interface that can define expected calls and returned values.
  3. Inject mocks: In test functions, replace actual dependencies with mocks.

Practical Case

Consider the following function using the net/http package:

func GetPage(url string) (*http.Response, error) {
    client := http.Client{}
    return client.Get(url)
}
Copy after login

To test this function, we need to mockhttp.Client because it is an external dependency.

Use GoogleMock for simulation

You can use the GoogleMock library for simulation. First, we define the interface to be mocked:

type MockClient interface {
    Get(url string) (*http.Response, error)
}
Copy after login

Then, we can create the mock using new(MockClient) and inject it in the test function:

import (
    "testing"

    "github.com/golang/mock/gomock"
)

func TestGetPage(t *testing.T) {
    ctrl := gomock.NewController(t)
    defer ctrl.Finish()

    client := mock.NewMockClient(ctrl)
    client.EXPECT().Get("http://example.com").Return(&http.Response{}, nil)

    resp, err := GetPage("http://example.com")
    assert.NoError(t, err)
    assert.NotNil(t, resp)
}
Copy after login

Use testify/mock for simulation

The testify/mock library also provides simulation functions. Usage examples are as follows:

import (
    "testing"

    "github.com/stretchr/testify/mock"
)

type MockClient struct {
    mock.Mock
}

func (m *MockClient) Get(url string) (*http.Response, error) {
    args := m.Called(url)
    return args.Get(0).(*http.Response), args.Error(1)
}

func TestGetPage(t *testing.T) {
    client := new(MockClient)
    client.On("Get", "http://example.com").Return(&http.Response{}, nil)

    resp, err := GetPage("http://example.com")
    assert.NoError(t, err)
    assert.NotNil(t, resp)
}
Copy after login

The above is the detailed content of Mocking techniques in Go function unit testing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!