mardi 15 octobre 2019

gmock: changing expectation on method arguments (without verifyAndClearExpectations)

In our project we have a big mock object, containing hundrets of methods.
It is a generated file and I can't do anything about that. (And I know it's rather wrong approach)

I want to write a test that expects some of these methods to be called with different arguments:

EXPECT_CALL(mock, foo(VAL_1)).Times(AtLeast(1));
...
EXPECT_CALL(mock, foo(VAL_2)).Times(AtLeast(1));
...
EXPECT_CALL(mock, foo(VAL_3)).Times(AtLeast(1));

The thing is that at one time there should be only one valid input to the method. How should I do it without using VerifyAndClearExpectations? (I want to still keep some other expectations set for different methods on the same mock).

I think I can achieve that with additional expectations as follows:

EXPECT_CALL(mock, foo(_).Times(0);
EXPECT_CALL(mock, foo(VAL_1)).Times(AtLeast(1));
...
EXPECT_CALL(mock, foo(_).Times(0);
EXPECT_CALL(mock, foo(VAL_2)).Times(AtLeast(1));
...
EXPECT_CALL(mock, foo(_).Times(0);
EXPECT_CALL(mock, foo(VAL_3)).Times(AtLeast(1));

Is that a correct approach or I'm missing something? What if code above will be executed in a loop of thousand cycles? Are those expectations held on some stack that I eventually consume or the foo('_').Times(0) will magically clear all the expectations above?

Maybe I shouldn't be affraid of using VerifyAndClearExpectations and just write several tests instead of one that within very similar (even same) scenarios but checking different expectations? In current scenario I have some expectations that should be valid during whole test and some will change several times in a way I described above.

Are there any good papers/guidelines for dealing with such and other tricky situations using GMock? I didn't found it's documentation useful for determining when should I use particular mechanisms of gmock.

Aucun commentaire:

Enregistrer un commentaire