我正在使用 Given/When/Then 模式使测试代码更清晰。由于我使用 C++ 编写这些测试,因此我选择使用 Google Test。通过测试,模式很清楚,因为我这样做:
TEST(TestFixture, TestName)
{
// Given
int a = 5;
int b = 6;
int expectedResult = 30;
// When
int result = Multiply(a, b);
// Then
EXPECT_EQ(expectedResult, result);
}
但是有了模拟,它就不再清楚了,因为在给定部分出现了一些期望。Given 部分假设是一个设置步骤。请看一个例子:
TEST(TestFixture, TestName)
{
// Given
int a = 5;
int b = 6;
int expectedResult = 30;
MightCalculatorMock mock;
EXPECT_CALL(mock, multiply(a,b))
.WillOnce(Return(expectedResult));
// When
int result = Multiply(mock, a, b);
// Then
EXPECT_EQ(expectedResult, result);
}
这种方法正确吗?Given/When/Then 注释应该如何放在测试代码中,在哪里?