首页 > 文章列表 > 如何模拟调用期望以实现表驱动测试覆盖

如何模拟调用期望以实现表驱动测试覆盖

412 2024-02-08
问题内容

在进行表驱动测试时,我使用 mockery 生成的一些模拟,并设置一些方法调用期望,这些期望取决于每个测试用例的数据集中提供的数据。我面临的问题是,模拟调用始终返回第一个测试用例中期望的结果集,而不是为执行的测试用例定义的结果集。

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)
        })
    }
}

当我运行此测试时,第二种情况失败,因为结果是 1 而不是预期的 2 ,我可以看到问题是模拟方法返回 1 (为第一个测试用例设置的值)而不是 2 (为当前测试用例设置的值)。

知道如何解决这个问题吗?


正确答案


这可能不是最优雅的解决方案,我想知道是否还有其他方法可以做到这一点,但目前,我已经找到了这个解决方案。它包括为表驱动测试运行的每个子测试生成一个新的模拟,因此在每个子测试中,我们使用一个全新的模拟实例,该实例没有从先前的子测试中设置任何期望。考虑到我使用 testify.suite 来组织和处理我的测试,这样做就像在每个子测试中手动调用 s.setuptest() 方法一样简单:

// 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)
        })
    }
}