Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update mockery's andReturn

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.

like image 305
Ivan Toncev Avatar asked Oct 31 '22 15:10

Ivan Toncev


1 Answers

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.

like image 85
Alister Bulman Avatar answered Nov 15 '22 05:11

Alister Bulman