简体   繁体   English

如何使用不同的谷歌模拟运行相同的谷歌测试用例?

[英]How to run the same google test case with different google mocks?

There are several test cases which was using a service.有几个使用服务的测试用例。 The test cases are written with google test.测试用例是用 google test 编写的。 The service can be of different types, so I have mocked each one with google mock.该服务可以是不同的类型,所以我用 google mock 模拟了每一个。 How do I write the tests in such a way so they can be run with different mocks without writing the same tests again and again with different mocks?我如何以这种方式编写测试,以便它们可以使用不同的模拟运行,而无需使用不同的模拟一次又一次地编写相同的测试?

The same test case for 2 different mocks are written like this: 2 个不同模拟的相同测试用例编写如下:

// For mock A
TEST_F(MockASampleTest, sample_test_case)
{
    EXPECT_CALL(mockA, mockAFunc(_))
        .Times(1)
        .WillOnce(Return(mockARetVal));
    EXPECT_EQ(testObj.testFunc(), 32);
}

// For mock B
TEST_F(MockBSampleTest, sample_test_case)
{
    EXPECT_CALL(mockB, mockBFunc(_))
        .Times(1)
        .WillOnce(Return(mockBRetVal));
    EXPECT_EQ(testObj.testFunc(), 32);
}

So, the problem is that the EXPECT_CALLS are different for different mocks due to different method names and return values of the mocks.因此,问题在于,由于模拟的方法名称和返回值不同,不同模拟的 EXPECT_CALLS 是不同的。 How can I combine these 2 test cases into one?如何将这 2 个测试用例合二为一?

If you use GoogleMock framework to create and control mocks during the tests' execution, you can specify (using eg EXPECT_CALLS ) how the mock should behave.如果您在测试执行期间使用 GoogleMock 框架创建和控制模拟,您可以指定(使用例如EXPECT_CALLS )模拟应该如何表现。

The object under tests shouldn't care about what type of mock is used in given tests if you use Dependency Inversion Principle from SOLID (ie there should be no code that depend on mock type in the class under test; the tested class should depend on interface, not concrete implementation)如果您使用 SOLID 中的依赖倒置原则,被测 object 不应该关心在给定测试中使用什么类型的模拟(即在被测 class 中不应该有依赖模拟类型的代码;经过测试的 ZA2F2ED4F8EBC2CBBDZC21 应该依赖于接口,而不是具体实现)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM