Skip to content
Advertisement

How to mock Eloquent Model in Laravel 8 when calling a route in PHPUnit

I’m trying to mock a method in the Model so that I am able to test the controller api endpoint with different scenarios.

I am currently using Laravel 8 with PhpUnit and Mockery package. In the route I am using Route model binding.

api.php

JavaScript

ProjectController.php

JavaScript

ProjectControllerTest.php

JavaScript

I am currently getting this error

JavaScript

I have tried other options like makePartial() instead, however it results in the following error as well

JavaScript

Any help would be much appreciated, thank you

Advertisement

Answer

You can try the following. I have changed the controller response slightly to make it work with assertJson().

web.php

JavaScript

ProjectController

JavaScript

ProjectControllerTest

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement