Christian Kaal
Christian Kaal

Reputation: 232

Laravel - Faking route in PHPUnit

I've been writing some tests in PHPUnit (version 7.4.3), but in some files in my project (a Laravel 5.7.12 JSON API) I need to use the route (/users/123/posts/456), but when PHPUnit calls the route, request()->getPathInfo() returns "".

Is there some way to fake this, or fill it with the correct data?

I am calling the route using $this->getJson(route('users.index')); in the test class.

Upvotes: 0

Views: 643

Answers (1)

millenion
millenion

Reputation: 1877

You should Mock the getJson method in order to return a fixture of your response.

As you are unit testing your app, you have to mock any external depedency or class to be sure you are only testing your current function.

Upvotes: 1

Related Questions