Laravel passport testing

I have a problem: I am using a Laravel passport for my api. I need to write tests. Whenever I use the WithoutMiddleware attribute in my tests, it disables the Implicit route model binding function that I use. Whenever I do not use this trait, I need to authenticate directly from my test. To do this, I need to create an API token through Passport.

The passport, however, must be installed before testing through artisan passport:install , because my tests use the DatabaseTransactions and DatabaseMigrations features. When I do this, the tests take a huge amount of time to run, and I feel that this is not the right way. There is no way to disable auth middleware only? Or any other ideas for this?

+5
source share
1 answer

A bit late for the party, but in accordance with this issue, this function is not currently supported, does not plan to change it at any time in the near future.

However, all Laravel test classes inherit the withoutMiddleware method, which you can use to disable middleware for specific methods . Not sure if this is useful to you, but just throwing it away:

 public function testBasicExample() { $this->withoutMiddleware(); $this->visit('/') ->see('Laravel 5'); } 

You can also check if tests are running in the middleware itself by calling the runningUnitTests() method on the application instance.

+1
source

Source: https://habr.com/ru/post/1258683/


All Articles