Support the ongoing development of Laravel.io →
Database Testing Installation
Last updated 2 years ago.
0

Are you using database transactions trait?

0

@simonhunt I removed it so I could observe the entries into the database.

0

@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?

0

@olimorris

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.

Last updated 7 years ago.
0

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.

Last updated 7 years ago.
0

Thanks Alex. Desperate soul saved. http://popkey.co/m/MMDxk-thumbs+up

0

Sign in to participate in this thread!

Eventy

Your banner here too?

olimorris olimorris Joined 31 Dec 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.