【发布时间】:2017-06-05 20:33:14
【问题描述】:
我正在尝试使用 DatabaseTransactions 特征测试我的 Laravel 系统。问题是它只有在对 TestCase 的所有测试都运行之后才回滚事务。是否可以为 TestCase 中的每个测试创建一个新的数据库实例?
此测试用例有时会返回全绿色,但有时不会。当它在写入时执行测试时,一切正常,但是当顺序颠倒时,第一个失败,因为之前创建了一个潜在客户。我能做什么?
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);
}
【问题讨论】:
-
你可以使用
setUp()方法。