@olimorris Sorry to come in again with more obvious questions but here goes:
Are you using a separate testing database? Are you doing a migrate:refresh before each test? Do you have any other tests lurking in other folders? What is actually in the database?
I have experienced the same problem. When you create the job:
$job = factory(App\Job::class)->create([
'org_id' => $org->id,
'user_id' => $user->id
]);
This lines inside the factory run anyway:
....'org_id' => factory(App\Organisation::class)->create()->id,
....'user_id' => factory(App\User::class)->create(['type' => 'organisation'])->id,
So it's creates the default Organization and User inside the database and only then it overwrites the 'org_id' and 'user_id' with the id's of models created outside the factory.
Hence you got 2 Organizations and 2 Users.
Gotta read the docs carefully, gentlemen; I just feel into this trap myself. Under "Relations & Attribute Closures", it clearly tells us to wrap the factory call in a closure. So instead of
'user_id' => factory(App\User::class)->create(['type' => 'organisation'])->id,
you should have
'user_id' => function () {
return factory(App\User::class)->create(['type' => 'organisation'])->id;
},
The latter does allow to overwrite attributes, the former doesn't.
Hope this helps some desperate soul out there.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community