Kbi*_*rjn 8 php unit-testing laravel-4
我在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."
);
}
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,测试应该在测试结束时创建一行并删除但是没有发生,我还应该做些什么.我知道当发生一些意外的异常但是我故意在最后调用它时会调用回滚,这不应该像我们认为的那样工作吗?
至少在 Laravel 5 中,您可以添加 DatabaseMigrations 特征:
use Illuminate\Foundation\Testing\DatabaseMigrations;
class MatchesControllerTest extends TestCase {
use DatabaseMigrations;
public function testForFun() {
// your test..
}
}
Run Code Online (Sandbox Code Playgroud)
该特征会创建和删除您在迁移中定义的数据库表,仅供您的测试使用。更多关于Laravel 测试文档中该特性的信息