Abhi
Abhi

Reputation: 15

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

Suppose I have to test the mock method GetSegment with exclusive value 0 and 1(two times).

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

Thanks,

Upvotes: 1

Views: 1092

Answers (1)

PiotrNycz
PiotrNycz

Reputation: 24347

Just do several WillOnce - one after another.

Like:

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

You can read in doc that WillOnce can be used several times in one EXPECT_CALL:

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

The simplified example that works:

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

}

You might also use 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"));

Upvotes: 1

Related Questions