Can I somehow update shouldReturn value for specific shouldReceive?
For example I have several tests that have use one return value, and only one that uses some other return value.
So I have put this in setUp:
$this->someDependency->shouldReceive('someMethod')->andReturn(0);
If I put this in that specific test case:
$this->someDependency->shouldReceive('someMethod')->andReturn(1);
It will be ignored and 0 will be returned.
I "fixed" this moving mocking to all tests and removing from setUp. But I don't like that code duplication.
This is mostly a matter of style. You can easily create a 'default' set of mocks in a helper function that is called by the test, and then a more explicit alternate version that sets them up differently.
You could also, in that helper, have a function parameter for a given value to return from a specific mock.
Some people believe that each test should be entirely independent from each other, other than some very common setup, and should do everything within itself. This does make it explicit as to what is going on (which very clear, and obvious what is going on) - but at a potential cost of code duplication. That is your choice however, if you prefer to not repeat yourself too much.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With