Refresh the mockery and return

Can I somehow update the shouldReturn value for a specific shouldReceive?

For example, I have several tests that use one return value, and only one that uses another return value.

So, I put this in setUp:

$this->someDependency->shouldReceive('someMethod')->andReturn(0); 

If I put this in this specific test case:

 $this->someDependency->shouldReceive('someMethod')->andReturn(1); 

It will be ignored and returns 0.

I “fixed” this move by mocking all tests and removing from setUp. But I don't like code duplication.

+5
source share
1 answer

This is basically a matter of style. You can easily create a set of "default" commands in a helper function called by the test, and then a more explicit alternative version that installs them differently.

This helper can also have a function parameter for a given value to return from a specific layout.

Some people believe that each test should be completely independent of each other, except for a very common installation, and should do everything in itself. This makes it explicit with respect to what is happening (which is very clear and obvious what is happening), but at the potential cost of duplicating the code. It is your choice, however, if you prefer not to repeat too much.

+2
source

Source: https://habr.com/ru/post/1236410/


All Articles