我正在编写测试代码,但在基本关系中我得到了一些意想不到的结果。我在内存中使用sqlite db。
这些测试与MySQL db一起运行良好。我想这与此迁移有关。
$tournament0 = factory(Tournament::class)
->create(['user_id' => $this->simpleUser->id]);
$this->simpleUser
是在setUp()
方法
现在,$this->simpleUser->tournaments
应该返回$tournament0
,但它是空的。
我的关系正在发挥作用,它没有被打破。此外,在浏览器中手动测试时,它可以工作。
使用phpunit时失败...
这是setUp()方法:
public function setUp()
{
parent::setUp();
$this->simpleUser = factory(User::class)->create(['role_id' => Config::get('constants.ROLE_USER')]);
}
失败的完整方法:
/** @test */
public function dashboard_check_initial_state()
{
Artisan::call('db:seed', ['--class' => 'CountriesSeeder', '--database' => 'sqlite']);
Artisan::call('db:seed', ['--class' => 'TournamentLevelSeeder', '--database' => 'sqlite']);
Artisan::call('db:seed', ['--class' => 'CategorySeeder', '--database' => 'sqlite']);
// Given
$this->logWithUser($this->simpleUser);
// Nothing has been created, default dash
$this->visit('/')
->see(trans('core.create_new_tournament'))
->dontSee(trans('core.congigure_categories'));
// Create 1 tournament
$tournament0 = factory(Tournament::class)->create(['user_id' => $this->simpleUser->id]);
$this->visit('/')
->seeInElement("p.text-muted", trans('core.no_tournament_created_yet'));
// Now configure 2/2 categories
$championship1 = factory(Championship::class)->create(['tournament_id' => $tournament0->id,'category_id'=>1]);
$championship2 = factory(Championship::class)->create(['tournament_id' => $tournament0->id,'category_id'=>2]);
factory(ChampionshipSettings::class)->create(['championship_id' => $championship1->id]);
factory(ChampionshipSettings::class)->create(['championship_id' => $championship2->id]);
$this->visit('/')
->seeInElement("span.text-muted", trans('core.congigure_categories'));
}
知道它可以是什么吗?