1

This is my setup

TEST_F(at_operator_selection_tests, select_operators_selects_operator_with_best_signal)
{
    // arrange
    InSequence s;

    // The next expectation will be referred to as exp#1
    EXPECT_CALL(_at_protocol_mock, select_operator(_,_)).WillOnce(Return(true));
    // Some other expectations left out intentionally here

    // The next expectation will be referred to as exp#2
    EXPECT_CALL(_at_protocol_mock, select_operator(_,_)).WillOnce(Return(true));
    // Some other expectations left out intentionally here

    _at_protocol_mock.select_operator(some_var, another_var); // will use exp#2
    _at_protocol_mock.select_operator(some_var, another_var); // will use exp#2, which is already saturated
}        

After reading this answer I now understand that the same expectations will be captured and looked up in reversed order. Hence, exp#1 will never be used, while exp#2 will be over saturated.

I could change my code as follows

TEST_F(at_operator_selection_tests, select_operators_selects_operator_with_best_signal)
{
    // arrange
    InSequence s;

    // The next expectation will be referred to as exp#1
    EXPECT_CALL(_at_protocol_mock, select_operator(_,_)).Times(2).WillOnce(Return(true));
    // Some other expectations left out intentionally here

    _at_protocol_mock.select_operator(some_var, another_var); // will use exp#2
    _at_protocol_mock.select_operator(some_var, another_var); // will use exp#2, which is already saturated
}  

This works, but it looks weird, since there are more expectations, and I want to have it clear that the select_operator expectation is used twice, in the context.

The answer above suggests to use the InSequence(s) function as follows

TEST_F(at_operator_selection_tests, select_operators_selects_operator_with_best_signal)
{
    // arrange
    InSequence s;

    // The next expectation will be referred to as exp#1
    EXPECT_CALL(_at_protocol_mock, select_operator(_,_)).Times(1).WillOnce(Return(true)).InSequence(s); // Compile error!!
    // Some other expectations left out intentionally here

    // The next expectation will be referred to as exp#1
    EXPECT_CALL(_at_protocol_mock, select_operator(_,_)).Times(1).WillOnce(Return(true)).InSequence(s);
    // Some other expectations left out intentionally here

    _at_protocol_mock.select_operator(some_var, another_var);
    _at_protocol_mock.select_operator(some_var, another_var);
 }

But this gives me a compile error I can't wrap my head around. I am simply clueless what it's trying to tell me here

/home/bp/dev/iobox/firmware/unit_tests/source/valinso/modules/at/operator_selection_tests.cpp:216:104: error: no matching function for call to ‘testing::internal::TypedExpectation<bool(const etl::string<40>&, unsigned char)>::InSequence(testing::InSequence&)’
  216 |         EXPECT_CALL(_at_protocol_mock, select_operator(_,_)).Times(1).WillOnce(Return(true)).InSequence(s);

How can I achieve what I want? I want to expect the same call multiple times in a sequence. I want googlemock to use these expectations in sequence, where it uses exp#2 once, and exp#1 the second time.

And, I would love to understand what the compiler is telling, why can't it compile the .InSequence(s) function call?

bas
  • 13,550
  • 20
  • 69
  • 146
  • As for the compile error: `s` it should be of type `Sequence` not `InSequence`. Not sure if that solves your case, but at least it should compile. Easy to miss, significant typo, I spent some time on it once, too ;P – alagner Sep 02 '22 at 11:18
  • IMO the code with `Times(2)` is clearer than two expectations. – 273K Sep 02 '22 at 14:36

0 Answers0