Zhen's avatar
Level 1

Unable to test BelongsToMany relationships

I'm working with a ProfileFactory that randomly attaches BelongsToMany relationships which works great when seeding the database. It also works fine when running the factory in tinker

$profile = factory(\App\Profile::class)->create();
$profile->spokenLanguages;

This will neatly print out all the randomly attached spokenLanguages.

However, whenever I run the factory in my phpunit test (using sqlite :memory: mode), these attached relationships turn up empty when debugging. This does not happen with hasMany relationships.

Am I running into a incorrect configuration of PHPunit or a certain limitation of sqlite?

0 likes
1 reply
Zhen's avatar
Zhen
OP
Best Answer
Level 1

It turns out that all my BelongsToMany relationships where attempting to link to tables that were not yet seeded. Fixed by running php (new DatabaseSeeder())->call(LocaleSeeder::class); before running the factory in the test.

Please or to participate in this conversation.