用于PHPunit的Laravel DatabaseTransactions无效

时间:2017-04-21 18:47:03

标签: laravel unit-testing testing phpunit

为现有API编写测试,在很多情况下数据库已被修改。我一直在做的事情如下:


ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

我现在遇到API根据外键创建或更新许多表的实例。这需要花费太多时间来手动重置每个表格。

Laravel提供的public function testPut() { //setup /*Copy an existing record and take its data as an array. * the function being tested will take an array of data * and create a new record. Using existing data guarantees * the data is valid. */ $customerObj = Customer::getInstance(); //regular instantiation disabled in this api $cust = ArCust::first()->toArray(); $oldNum = $cust['CUST_NO']; unset($cust['CUST_NO']); $custNo = rand(1, 9999999999999); //test /*put() creates a new customer record in the database and returns the object. */ $this->assertInternalType('object', $customerObj->put($custNo, $cust)); //cleanup /*manually remove the newly created record. */ ArCust::whereNam($cust['NAM'])->whereNotIn('CUST_NO', [$oldNum])->delete(); } 特性应该为您重置一切。但是,当我使用它时,我仍然在数据库中找到测试创建的记录。 以下是我如何使用它:

DatabaseTransaction

我错误地使用了这个吗?让class CustomerTest extends TestCase { use DatabaseTransactions; public function testPut() { //setup $customerObj = Customer::getInstance(); $cust = ArCust::first()->toArray(); $oldNum = $cust['CUST_NO']; unset($cust['CUST_NO']); $custNo = rand(1, 9999999999999); //test $this->assertInternalType('object', $customerObj->put($custNo, $cust)); } } 正常工作将节省大量时间,并使睾丸对其他人更具可读性。

2 个答案:

答案 0 :(得分:0)

从您的测试中看起来您没有打电话给const ROUTER_DATA: Routes = [ { path:'login', component: LoginComponent}, { path:'home', component: HomeComponent, children: [ { path:'unitsheet', component: unitComponent}, ]}, { path:'', redirectTo:'login', pathMatch: 'full'} ]

你尝试过这样的事吗?

beginDatabaseTransaction()

答案 1 :(得分:0)

问题是我们在config>中定义了多个数据库连接。数据库。

database.php conf文件中,我使用设置中定义的名称将默认连接更改为正确的数据库:

$connection = 'counterpoint';

DatabaseTransactions现在有效。

此解决方案的下一步是将每个测试的连接指向适当的数据库,而不是更改全局连接。