0

After update to laravel 8 my passport tests fail because of factory and for all test I get message like this:

BadMethodCallException: Call to undefined method Laravel\Passport\Client::factory()
knubbe
  • 1,132
  • 2
  • 12
  • 21
  • your previous question said all tests failed, now it is only passport tests? and this was a test that worked before the upgrade? – lagbox Sep 13 '20 at 22:33
  • yes, I adapt all factories that I have, modify some things. Now I have a problem with Client::factory() of Passport – knubbe Sep 13 '20 at 22:34
  • also perhaps you would like to resolve your last question you posted – lagbox Sep 13 '20 at 22:36
  • the `Laravel\Passport\Client` model does not implement the `HasFactory` trait so it won't have a `factory` method – lagbox Sep 13 '20 at 22:37
  • But in passport documentation (section testing) there is same factory call like any regular laravel model – knubbe Sep 13 '20 at 22:39
  • https://github.com/laravel/passport/blob/10.x/src/Client.php ... it does not use that trait and it does not have a `factory` method – lagbox Sep 13 '20 at 22:43
  • https://laravel.com/docs/8.x/passport#testing – knubbe Sep 13 '20 at 22:53
  • you can keep referencing that all you want, it does not change the fact that this does not have that method ... it would appear the docs are wrong – lagbox Sep 13 '20 at 22:54

2 Answers2

1

The Laravel\Passport\Client model does not use the HasFactory trait. You will have to call the factory directly:

Laravel\Passport\Database\Factories\ClientFactory::new()->count(3)->make();

Laravel 8.x Docs - Database Testing - Model Factories - Creating Models

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • I create factory directly but now for this code ``` $building = Building::factory()->create(); Passport::actingAsClient( PassportClientFactory::new()->create(), ); $response = $this->get('/api/buildings/'.$building->id); $response->assertStatus(200); $response->assertJsonFragment(['name' => $building->name]); ``` I got this: ``` Illuminate\Contracts\Container\BindingResolutionException: Target class [bindings] does not exist. ``` – knubbe Sep 13 '20 at 22:53
  • i dont understand what that means "but now for this code"? – lagbox Sep 13 '20 at 22:54
  • also you still have not resolved your last question you posted https://stackoverflow.com/questions/63870060/all-test-failing-after-update-from-laravel-7-to-laravel-8 – lagbox Sep 13 '20 at 23:04
  • use the stacktrace to help find where that error is coming from, would be hard to tell just based on that ... just put a single backtick at the beginning of the code and one at the end to format it in the comments `:)` – lagbox Sep 13 '20 at 23:08
0

I finally solve this issue by removing namespace for api routes in RoutesServiceProvider. I also use \Illuminate\Routing\Middleware\SubstituteBindings::class instead of bindings in api middleware groups (Kernel.php) and I use controller class in api routes

knubbe
  • 1,132
  • 2
  • 12
  • 21