Home  >  Article  >  Backend Development  >  How to override mock call expectations in table driven tests

How to override mock call expectations in table driven tests

WBOY
WBOYforward
2024-02-10 21:42:09840browse

How to override mock call expectations in table driven tests

#php editor Xiaoxin today introduces a method to override simulation call expectations in table-driven testing. Table-driven testing is an effective testing technology that can test in a data-driven manner and improve the maintainability and scalability of the code. In testing, we often need to mock call expectations to ensure that the code under test behaves as expected. This article will introduce in detail how to use table-driven testing to achieve the desired coverage of mock calls, helping developers better conduct unit testing.

Question content

When doing table-driven testing, I use some mocks generated by mockery and set some method call expectations that depend on each test case Data provided in the dataset. The problem I'm facing is that the mock call always returns the result set expected in the first test case and not the result set defined for the executed test case.

func (s *MyTestSuite) TestMySampleTest() {
    testCases := []struct {
        Name     string
        Requests []*service.CreateCredentialRequest
    }{
        {
            Name:     "first case",
            mockedResult: 1,
            expected: 1,
        },
        {
            Name:     "second case",
            mockedResult: 2,
            expected: 2,
        },
    }

    for _, tc := range testCases {
        s.Run(tc.Name, func() {
            s.someMock.On("SomeMethodCall", mock.Anything).Return(tc.mockedResult)

            result := s.SUT.SomeMethodThatCallsTheMockedObe()

            s.Equal(expected, result)
        })
    }
}

When I run this test, the second case fails because the result is 1 instead of the expected 2, I can see that the problem is that the mocked method returns 1 (the value set for the first test case) instead of 2 (the value set for the current test case).

Any idea how to solve this problem?

Workaround

This may not be the most elegant solution and I was wondering if there were any other ways to do this, but for now, I've found this solution. It consists of generating a new mock for each subtest run by the table driven test, so in each subtest we use a completely new mock instance that does not set any expectations from the previous subtest. Considering that I use testify.suite to organize and handle my tests, doing so is as simple as manually calling the s.setuptest() method in each subtest:

// SetupTest is executed before every test is run, I instantiate the SUT and 
// its dependencies here.
func (s *MyTestSuite) SetupTest() {
    // Instantiate the mock
    s.someMock = mocks.NewSomeMock(s.T())
    // Instantiate the SUT, injecting the mock through the constructor function
    s.SUT = NewSUT(s.someMock)
}

func (s *MyTestSuite) TestMySampleTest() {
    testCases := []struct {
        Name     string
        Requests []*service.CreateCredentialRequest
    }{
        // test cases here 
    }

    for _, tc := range testCases {
        s.Run(tc.Name, func() {
            // Manually calling s.SetupTest() to generate new instances of the mocks in every subtest.
            // If we don't do this, the mock will always return the first expectation set (the one set for the first test case).
            s.SetupTest()

            // Here comes the logic of the text as we had it before
            s.someMock.On("SomeMethodCall", mock.Anything).Return(tc.mockedResult)

            result := s.SUT.SomeMethodThatCallsTheMockedObe()

            s.Equal(expected, result)
        })
    }
}

The above is the detailed content of How to override mock call expectations in table driven tests. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete