Database 使用phpUnit进行Laravel 5测试,无需重置我的数据库
有没有办法创建一个setUp()和tearDown()方法,告诉laravel类似“在数据库中创建所有插入,但当您完成所有测试时,删除所有插入而不重置数据库”的内容 我需要做如下测试:Database 使用phpUnit进行Laravel 5测试,无需重置我的数据库,database,unit-testing,laravel-5,phpunit,laravel-migrations,Database,Unit Testing,Laravel 5,Phpunit,Laravel Migrations,有没有办法创建一个setUp()和tearDown()方法,告诉laravel类似“在数据库中创建所有插入,但当您完成所有测试时,删除所有插入而不重置数据库”的内容 我需要做如下测试: /** @test */ public function testRegistration(){ $account = [ 'name' => 'test', 'email' => 'test@gmail.com', 'password' => '1
/** @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
,它会工作,如果我再次运行,它当然会返回一个错误,告诉我无法使用此电子邮件,因为数据库中已经存在此电子邮件
问题是我不能简单地重置我的数据库,我已经在数据库中插入了12000行,我不能创建测试数据库,因为我需要将这12000行插入到我的应用程序中
我还没有找到任何可以使用的信息,我所能找到的只是“让您的测试调用迁移::刷新并发送4分钟以再次填充您的表”,但我相信有可能找到更好的解决方案
另外,我应该把setUp()方法放在哪里,在哪里调用它
谢谢。请参阅和此(处理事务)
此特性仅在事务中包装默认数据库连接
换句话说,插入将是“伪造的”,因此您不必每次都删除它
另一个选项是添加另一个测试,在该测试中删除所做的每个插入
User::where('name', 'test')->where('email','test@gmail.com')->delete();
谢谢,正是我需要的!