Mockery throws NoMatchingExpectationException while testing a job.
I am trying to test a job that calls a method from a service. The code for job and test looks something like below.
But, Mockery always throwing Mockery\Exception\NoMatchingExpectationException and saything that Either the method was unexpected or its arguments matched no expected argument list for this method. I have tried with withArgs as well, but it is still throwing the error.
class TheJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public int $tries = 5;
public function __construct(
public int $total
) {
}
/**
* Execute the job.
*
* @return void
*/
public function handle(
Service $service,
): void {
$service->execute(
$this->total
);
}
}
public function test_that_params_are_correct()
{
Bus::fake();
$service = Mockery::mock(Service::class);
$job = new TheJob($this->total);
$service->expects('execute')->with(Mockery::on(function (
int $total,
) {
return true;
}));
$job->handle($service);
}
It looks like you are missing the argument in the expects method. The execute method in the Service class takes an int as an argument, so you need to include that in the expects method.
Try changing your code to this:
public function test_that_params_are_correct()
{
Bus::fake();
$service = Mockery::mock(Service::class);
$job = new TheJob($this->total);
$service->expects('execute')->with($this->total);
$job->handle($service);
}