【问题标题】:Issue with $softDelete in model testing模型测试中的 $softDelete 问题
【发布时间】:2013-08-02 04:26:45
【问题描述】:

我在 laravel 4 中使用 jeffrey 方式的 laravel-test-helper 包来测试我的模型。当我使用

protected $softDelete = true;

在我的模型中,以下断言失败:

public function testMyModel()
{

    $obj = Factory::create('Modelname');
    Assert::equals(1,$obj->count());
}

当我在没有 $softDelete 变量的情况下运行测试时,它工作正常。 有什么问题的线索吗? 这是 phpunit 命令的堆栈跟踪,如果有帮助的话:

/var/www/project/vendor/way/laravel-test-helpers/src/Way/Tests/TestFacade.php:41
/var/www/project/vendor/way/laravel-test-helpers/src/Way/Tests/TestFacade.php:25
/var/www/project/vendor/way/laravel-test-helpers/src/Way/Tests/TestFacade.php:55
/var/www/project/app/tests/models/ModelTest.php:13
/var/www/project/app/tests/models/ModelTest.php:13

【问题讨论】:

    标签: unit-testing model laravel laravel-4


    【解决方案1】:

    Factory::create() 将为您的模型创建随机变量。因为您使用的是“softDelete” - 表中有一个名为“deleted_at”的字段。所以工厂往里面放数据,Laravel 认为记录被删除,所以->count() 返回 0。

    您需要将 deleted_at 的出厂默认设置显式设置为 null,如下所示:

    $obj = Factory::create('Modelname', array('deleted_at' => null));
    

    【讨论】:

      猜你喜欢
      • 2014-10-29
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      相关资源
      最近更新 更多