我如何用两个不同的参数值调用我的模拟方法两次

How can i call my mock method two times with two different argument value

假设我必须使用 独占 值 0 和 1(两次)测试模拟方法 GetSegment。

EXPECT_CALL(*mock.get(), GetSegment(refrenceId, _, _, _)).
WillOnce(DoAll(SetArgReferee<1>(numSegment), SetArgPointee<2>(points), SetArgPointee<3>(**exclusive**)));

谢谢,

做几个 WillOnce - 一个接一个。

喜欢:

EXPECT_CALL(*mock.get(), GetSegment(refrenceId, _, _, _))
   .WillOnce(DoAll(SetArgReferee<1>(numSegment), SetArgPointee<2>(points), SetArgPointee<3>(0))).
   .WillOnce(DoAll(SetArgReferee<1>(numSegment), SetArgPointee<2>(points), SetArgPointee<3>(1)));

你可以在doc中读到WillOnce可以一次使用多次EXPECT_CALL:

EXPECT_CALL(mock_object, method(matchers)) .WillOnce(action) *

有效的简化示例:

class MockMM
{
public:
    MOCK_METHOD4(GetSegment, void(int refrenceId, int, int, int* a));
};

TEST(A, A)
{
    MockMM mock;
    EXPECT_CALL(mock, GetSegment(1, _, _, _))
            .WillOnce(SetArgPointee<3>(0))
            .WillOnce(SetArgPointee<3>(1));

    int a;
    int b;
    mock.GetSegment(1, 1, 0, &a);
    mock.GetSegment(1, 0, 1, &b);
    ASSERT_EQ(0, a);
    ASSERT_EQ(1, b);

}

您也可以使用 sequences:

When you have a long chain of sequential expectations, it's easier to specify the order using sequences, which don't require you to given each expectation in the chain a different name. All expected calls in the same sequence must occur in the order they are specified.

using ::testing::Sequence;
Sequence s1, s2;
...
EXPECT_CALL(foo, Reset())
    .InSequence(s1, s2)
    .WillOnce(Return(true));
EXPECT_CALL(foo, GetSize())
    .InSequence(s1)
    .WillOnce(Return(1));
EXPECT_CALL(foo, Describe(A<const char*>()))
    .InSequence(s2)
    .WillOnce(Return("dummy"));