Skip to content
Advertisement

Laravel: Test not Using Mocked Method

I want getNumber to return 200 in the test case, but when I run the test and inspect the result with $response->dump() I can see that the mock is not overriding the method as it is showing array:1 [0 => 100]. What am I missing?

Controller:

JavaScript

Model:

JavaScript

Test:

JavaScript

I am using Laravel 8 and referencing this documentation: https://laravel.com/docs/8.x/mocking#mocking-objects

Advertisement

Answer

In Laravel you have to use the container to mock classes.

JavaScript

In constructors or handle methods, if you inject your classes there it will automatically use the container. If put into constructors or classes, remember the resolution of the class with the constructor, have to use the container.

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