我在Laravel测试中有这种疲惫的行为.让我告诉你我的测试.
<?PHP class MatchesControllerTest extends TestCase { public function setUp() { parent::setUp(); DB::beginTransaction(); } public function tearDown() { DB::rollBack(); } public function testForFun() { $title = 'Yay Great Post'; // "Create" post Post::create(compact('title')); $crawler = $this->client->request('GET','posts'); $this->assertEquals( 1,count($crawler->filter("body:contains('{$title}')")),"Expected to see the text '{$title}' within a body element." ); } }
理想情况下,测试应该在测试结束时创建一行并删除但是没有发生,我还应该做些什么.我知道当发生一些意外的异常但是我故意在最后调用它时会调用回滚,这不应该像我们认为的那样工作吗?
至少在Laravel 5中,您可以添加DatabaseMigrations特征:
原文链接:https://www.f2er.com/laravel/134044.htmluse Illuminate\Foundation\Testing\DatabaseMigrations; class MatchesControllerTest extends TestCase { use DatabaseMigrations; public function testForFun() { // your test.. } }
该特性创建并删除您在migrations中为测试而定义的数据库表.更多关于Laravel testing documentation的特征