有没有办法制作一个 setUp() 和 tearDown() 方法来告诉 laravel 类似“在数据库中进行所有这些插入,但是当你完成所有测试后,删除所有插入而不重置数据库”
我需要做这样的测试:
/** @test */
public function testRegistration(){
$account = [
'name' => 'test',
'email' => 'test@gmail.com',
'password' => '123451234'
];
$this->visit('/register')
->type($account['name'],'name')
->type($account['email'],'email')
->type($account['password'],'password')
->type($account['password'],'password_confirmation')
->press('Register')
->seePageIs('/home');
}
首先,我可以运行 phpunit
它会工作,如果我再次运行,它当然会返回一个错误告诉我我不能使用这封电子邮件,因为它已经在数据库中。
问题是我不能简单地重置我的数据库,我已经在我的数据库中插入了 12.000 行并且我不能创建一个 test_database 因为我需要将这 12.000 行插入到我的应用程序中是有意义的。
我还没有找到任何我可以使用的信息,我能找到的只是“让你的测试调用迁移::刷新并发送 4 分钟以再次填充你的表”,但我确信这是可能的找到更好的解决方案!
另外,我应该把我的 setUp() 方法放在哪里,我应该在哪里调用它?
谢谢。
最佳答案
参见 this还有这个documentation (处理事务)。
This trait will only wrap the default database connection in a transaction.
换句话说,插入将是“伪造的”,因此您无需每次都将其删除。
另一种选择是添加另一个测试,您可以在其中删除您所做的每个插入。
User::where('name', 'test')->where('email','test@gmail.com')->delete();
关于database - 使用 phpUnit 进行 Laravel 5 测试而不重置我的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40687570/