首页  >  文章  >  后端开发  >  如何在表驱动测试中覆盖模拟调用期望

如何在表驱动测试中覆盖模拟调用期望

WBOY
WBOY转载
2024-02-10 21:42:09844浏览

如何在表驱动测试中覆盖模拟调用期望

php小编小新今天为大家介绍一种在表驱动测试中覆盖模拟调用期望的方法。表驱动测试是一种有效的测试技术,能够通过数据驱动的方式来进行测试,提高代码的可维护性和可扩展性。在测试中,我们经常需要模拟调用期望,以确保被测试的代码行为符合预期。本文将详细介绍如何使用表驱动测试来实现模拟调用期望的覆盖,帮助开发人员更好地进行单元测试。

问题内容

在进行表驱动测试时,我使用 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)
        })
    }
}

以上是如何在表驱动测试中覆盖模拟调用期望的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除