Ask Your Question
1

What is the process of configuring Mockery to provide output depending on earlier method invocations?

asked 2021-08-11 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-08-17 09:00:00 +0000

lakamha gravatar image

To configure Mockery to provide output depending on earlier method invocations, we need to use the shouldReceive method with the andReturn or andThrow methods.

Here is an example:

$mock = Mockery::mock(SomeClass::class);
$mock->shouldReceive('method1')->andReturn('result');

// Call the first method
$result1 = $mock->method1(); // returns 'result'

$mock->shouldReceive('method2')->with($result1)->andReturn('another result');

// Call the second method, passing the result from the first method
$result2 = $mock->method2($result1); // returns 'another result'

In this example, we create a mock of SomeClass and configure it to return 'result' when its method1 is called. We then call method1 to get the result, which we use as an argument when configuring method2. We configure method2 to return 'another result' when called with the result of method1. We call method2 with the result of method1 as an argument, and Mockery returns 'another result'.

By using these methods, we can simulate complex behavior in a controlled way and test our code thoroughly.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-11 11:00:00 +0000

Seen: 7 times

Last updated: Aug 17 '21