在 Laravel 中运行测试时(php artisan 测试),出现错误“重复的列名:created_at”。只有当我在迁移目录中添加字段时才会发生这种情况。运行 Laravel 测试时我可以忽略特定文件吗?还是有另一种方法来解决这个问题?
迁移: 如果我只有这张桌子,测试工作正常:
2021_06_18_134444_create_users_tables.php
创建此迁移后,测试失败:
2021_07_08_135544_add_timestamps_to_all_tables.php
一个简单的测试:
use RefreshDatabase;
public function test_redirect_to_home_page_after_login()
{
$user = User::factory()->make([
'name' => 'Test',
'email' => '[email protected]',
'password' => bcrypt('123456')
]);
$response = $this->post('login', [
'name' => 'Test',
'email' => '[email protected]',
'password' => '123456'
]);
$response->assertRedirect('/');
$response->assertSessionHasErrors();
}
你可以用这样的条件包装你的
up()
和 down()
函数。
if (app()->runningUnitTests()) {
// your function content
}