0

Is it possible to ignore unexpected method calls for Turtle Mock? During my test the mocked method is called multiple times. I want to check only one invocation with specific parameters per test. Now I have to write one enormous test where I have to write all method invocations.

w00drow
  • 468
  • 2
  • 11
  • Do you have some code to show? From a cursory scan of the docs I'd guess you can get your free pass using MOCK_RESET/MOCK_VERIFY explicitly. If you add a small example where you are stuck, we could have a look – sehe Apr 18 '21 at 01:08

1 Answers1

2

The expectation selection algorithm describes how you can set multiple invocations:

Each method call is then handled by processing the expectations in the order they have been defined :

  • looking for a match with valid parameter constraints evaluated from left to right
  • checking that the invocation count for this match is not exhausted

So if you set the one you expect and a generic one such as

MOCK_EXPECT( v.display ).once().with( 0 );
MOCK_EXPECT( v.display );

it should quiet the other calls while still making sure the one you care about will be fulfilled.

Now if you wanted to enforce the order of the calls, for instance to make sure the one you’re interested in happens first, you would have to use a sequence, such as

mock::sequence s;
MOCK_EXPECT( v.display ).once().with( 0 ).in( s );
MOCK_EXPECT( v.display ).in( s );
mat007
  • 905
  • 8
  • 16
  • Thanks for the hint. But "once" seems to be really needed because otherwise this test is green: `class A { public: virtual void f(int a) = 0; }; MOCK_BASE_CLASS(AMock, A) { public: MOCK_METHOD(f, 1, void(int)) }; void test(A* a) { a->f(5); a->f(66); a->f(7); } BOOST_AUTO_TEST_CASE(StackOverflow_SimpleTest) { AMock aMock; MOCK_EXPECT(aMock.f).with(6); MOCK_EXPECT(aMock.f); test(&aMock); }` – w00drow Apr 18 '21 at 07:33
  • Ah yes, good point, without `once()` then «no call» is valid. I’ll update my answer. – mat007 Apr 18 '21 at 15:23