4.3. 测试中的数据迁移
本节说明
在上一节测试的结果是,users
表不存在,这节我们来让该问题通过。
测试时运行数据库迁移
Laravel
为我们准备了一个RefreshDatabase
的 trait
,当每次运行测试之前,会运行数据库迁移,创建表;运行完测试之后,回滚数据库迁移,删除表。所以我们只需使用该trait
即可:
tests/Feature/ViewQuestionsTest.php
<?php
namespace Tests\Feature;
use App\Question;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;<...>