Answer the question
In order to leave comments, you need to log in
How to set mock relations in Laravel?
I deal with tests in Laravel, I ran into a problem:
There is a model (model 1) - I can easily replace it with an instance from the factory
But this model has a hasMany-relation (model 2), it would also need to be replaced
How can this be organized?
Answer the question
In order to leave comments, you need to log in
class AlbumTest extends TestCase
{
use DatabaseMigrations;
public function test()
{
$album = factory('Album')->create();
$songs = new Collection([
factory('Song')->make(['duration' => 291]),
factory('Song')->make(['duration' => 123]),
factory('Song')->make(['duration' => 100]),
]);
$album->songs()->saveMany($songs);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question