Robert Basic has a new post to his site where he shows how to use the Mockery unit testing too to return different values for different arguments. Fortunately there’s something already built into the tool to help handle this.
Sometimes when working with Mockery mock objects, we want to tell a mocked method to return different values for different arguments. It is a rare occasion when I need this feature, but every time I need it, I’m happy it’s there.
The feature that allows us to return different values based on arguments is the andReturnUsing Mockery method, which takes a closure as an argument.
He includes examples of the use of this
andReturnUsing method in mocks and showing that there’s more than one way to accomplish the same kind of goal. While this is a useful method to use when needed he points out that refactoring the code under test is probably a better way to go instead.