-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to ignore some mock method calls? #126
Comments
Unfortunately there is no way to ignore those calls. The semantic of Consider the alternative: You want to make sure only method Not only that: Your method Keep in mind that mock objects don't need to be derived classes, so things like "just call the parent method" don't work in the general case. This gives a lot of flexibility to the user but also makes it a bit harder at some points on the library and user side. |
Thanks Alexander, I can understand what you mean. What I had in mind was something in the MOCK_BASE_CLASS like: MOCK_METHOD_DEFAULT where a return value is specified and that means if no MOCK_EXPECT is defined in the test then this one is used. |
I don't think that would be easy enough to implement to be worth the work because expectations are stored in instances of the class. You can have a method I could imagine a macro In any case I still lean towards the original design of being explicit in the test. Every expected call must be marked as such. This is the safest behavior at least. |
Your idea to have a default expectation that can be replaced by an explicit MOCK_EXPECT makes sense. I agree with you to be explicit in the test but how else would you solve my case?
In case 1, it does not change much wrt MOCK_EXPECT_DEFAULT because in the absence of this I have to implement a base class where default values are returned for each method (a Stub). Each different mock will inherit then from the Stub. So, the same idea behind MOCK_EXPECT_DEFAULT but without the flexibility to have just one mock class. Case 2 is worst from my point of view because, it is true to be explicit in the tests but even more tests should be simple, clear and easy to read. |
I guess different people will have different opinions on the "clear" part:
Having this with some variations in each test method is very clear in what happens: Those methods may be called, we don't care how and how often. This is mostly what "implement a base class where default values are returned for each method (a Stub)." also achieves but with more flexibility. Others might argue that this makes it easy to forget removing one of the "Common" setups when adding one of the "Important" ones. This is what Having defaults defined for the class won't be easy to implement as the state needs to be stored somewhere and it would be quite messy to integrate some kind of default state without running into any corner case that might break someone in a specific situation. |
Supposing I have two tests that use the same mock object. In both tests there is a call that causes the call of both methods of the mock. The point is that I want to verify just one mock method for each test. Is there a way to ignore the other mock method calls?
I suppose one option is to add in each test a MOCK_EXPECT of the other method too but what if the methods of the mock are 10? It is not a good idea to add nine MOCK_EXPECT for all the other methods not strictly involved in the current test. Another option is to create a different mock one for each test, but also this way is a little annoying. Is there a clean way to solve this problem?
I the previous example toTest.update() calls both a() and b() of the mock.
Thanks
The text was updated successfully, but these errors were encountered: