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.
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
Asked: 2021-08-11 11:00:00 +0000
Seen: 7 times
Last updated: Aug 17 '21
How can the output of a loop within a function be visualized using Rshiny?
What is the process of gathering an iterator and storing it as a Result?
How can SolidJS context be utilized in an asynchronous function?
When I use .target.innerHTML, why does my input consistently give a result of 0?
Is there a simpler way to achieve the same result in Excel rather than having a lengthy formula?
How can Excel VBA identify the row that contains the formula result of #N/A?