使用代码进行Laravel测试:单元测试模型的save()方法不会持久化

时间:2017-02-10 02:58:04

标签: php laravel-5.3 codeception

这是一个测试数据库持久性的简单单元测试,无法使其工作。继续收到以下错误消息。

看起来save()方法无法将记录插入testdb,我使用测试数据库中的现有记录更改断言,它可以工作。

$ this-> tester-> seeInDatabase('status',['name'=>'Status4']);

花了很多时间进行故障排除,没有任何线索。

谢谢, 杰克

    There was 1 failure:

    ---------
    1) UserTest: Saving status
     Test  tests\unit\App\UserTest.php:testSavingStatus
    No matching records found for criteria {"name":"Status4"} in table statuses
    Failed asserting that 0 is greater than 0.
    #1  Codeception\Module\Db->seeInDatabase
    #2  C:\MAMP\htdocs\noc\tests\_support\_generated\UnitTesterActions.php:3725
    #3  C:\MAMP\htdocs\noc\tests\unit\App\UserTest.php:34
    #4  App\UserTest->testSavingStatus  

    public function testSavingStatus()
        {
            $status = new Status();
            $status->setName('Status4');
            $status->save();

            $this->assertEquals('Status4', $status->name);
            $this->tester->seeInDatabase('statuses', ['name' => 'Status4']);
        }

   actor: Tester
    paths:
        tests: tests
        log: tests/_output
        data: tests/_data
        support: tests/_support
        envs: tests/_envs
    settings:
        bootstrap: _bootstrap.php
        colors: false
        memory_limit: 1024M
    extensions:
        enabled:
            - Codeception\Extension\RunFailed
    modules:
        config:
            Db:
                dsn: 'mysql:host=localhost;dbname=testdb'
                user: 'root'
                password: 'root'
                dump: tests/_data/dump.sql
                populate: true
                cleanup: false
                reconnect: true

1 个答案:

答案 0 :(得分:0)

Db模块无法查看记录,因为它使用单独的连接而Laravel5模块包装了测试in transaction

  

cleanup:boolean,默认为true - 所有数据库查询都将在事务中运行,该事务将在每次测试结束时回滚。

使用Laravel5模块的seeRecord方法,或者如果您有其他理由使用Db模块,请禁用清理。