每次测试后的数据库事务
DatabaseTransactions after each test
我正在尝试使用 DatabaseT运行sactions 特性测试我的 Laravel 系统。问题在于它仅在 TestCase 上的所有测试都为 运行 之后才回滚 t运行saction。是否可以为 TestCase 中的每个测试创建一个新的数据库实例?
这个测试用例有时return全是绿色的,但有时不是。当它按照编写的方式执行测试时,一切正常,但是当顺序颠倒时,第一个失败,因为之前创建了一个潜在客户。我能做什么?
public function testPotentialLeads()
{
factory(Lead::class)->create(['lead_type' => LeadType::POTENTIAL]);
factory(Lead::class)->create();
factory(Lead::class)->create();
$potential_leads = Lead::potentials()->get();
$this->assertEquals(1, $potential_leads->count());
$this->assertEquals(3, Lead::all()->count());
}
public function testAnotherLeadFunction()
{
$lead = factory(Lead::class)->create();
$this->assertTrue(true);
}
首先,这个测试并不是真正的测试:$this->assertTrue(true);
。如果您想测试潜在客户是否已创建,您应该使用 $this->assertTrue($lead->exists());
如果你想运行单元测试按一定的顺序,你可以使用@depends注解
DatabaseTransactions
特性在每次测试后回滚,而不是在所有测试后回滚
如果你想在每次测试前后迁移和迁移回滚而不是将它们包装到事务中,你可能想使用 DatabaseMigrations
特性
如果您想使用自定义设置和拆卸方法,请使用 afterApplicationCreated
和 beforeApplicationDestroyed
方法注册回调
我发现了我的错误。它失败了,因为当我这样做时:
factory(Lead::class)->create(['lead_type' => LeadType::POTENTIAL]);
factory(Lead::class)->create();
factory(Lead::class)->create();
$potential_leads = Lead::potentials()->get();
$this->assertEquals(1, $potential_leads->count());
$this->assertEquals(3, Lead::all()->count());
使用随机 LeadType(通过模型工厂)生成了两个潜在客户,因此在创建更多潜在潜在客户时进行了一些尝试。
我正在尝试使用 DatabaseT运行sactions 特性测试我的 Laravel 系统。问题在于它仅在 TestCase 上的所有测试都为 运行 之后才回滚 t运行saction。是否可以为 TestCase 中的每个测试创建一个新的数据库实例?
这个测试用例有时return全是绿色的,但有时不是。当它按照编写的方式执行测试时,一切正常,但是当顺序颠倒时,第一个失败,因为之前创建了一个潜在客户。我能做什么?
public function testPotentialLeads()
{
factory(Lead::class)->create(['lead_type' => LeadType::POTENTIAL]);
factory(Lead::class)->create();
factory(Lead::class)->create();
$potential_leads = Lead::potentials()->get();
$this->assertEquals(1, $potential_leads->count());
$this->assertEquals(3, Lead::all()->count());
}
public function testAnotherLeadFunction()
{
$lead = factory(Lead::class)->create();
$this->assertTrue(true);
}
首先,这个测试并不是真正的测试:
$this->assertTrue(true);
。如果您想测试潜在客户是否已创建,您应该使用$this->assertTrue($lead->exists());
如果你想运行单元测试按一定的顺序,你可以使用@depends注解
DatabaseTransactions
特性在每次测试后回滚,而不是在所有测试后回滚如果你想在每次测试前后迁移和迁移回滚而不是将它们包装到事务中,你可能想使用
DatabaseMigrations
特性如果您想使用自定义设置和拆卸方法,请使用
afterApplicationCreated
和beforeApplicationDestroyed
方法注册回调
我发现了我的错误。它失败了,因为当我这样做时:
factory(Lead::class)->create(['lead_type' => LeadType::POTENTIAL]);
factory(Lead::class)->create();
factory(Lead::class)->create();
$potential_leads = Lead::potentials()->get();
$this->assertEquals(1, $potential_leads->count());
$this->assertEquals(3, Lead::all()->count());
使用随机 LeadType(通过模型工厂)生成了两个潜在客户,因此在创建更多潜在潜在客户时进行了一些尝试。