Laravel 5.1雄辩是单位测试中的填充()差异 [英] Laravel 5.1 Eloquent isFillable() discrepancy in unit tests

查看:128
本文介绍了Laravel 5.1雄辩是单位测试中的填充()差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在非测试情况下,我得到以下输出,这是正确的,因为模型只允许某些可填充字段:

  $ m = new Model(); 
dd($ m-> isFillable('nonexistent_field')); - > false

但是,如果我在单元测试中做同样的事情:

  $ m = new Model(); 
dd($ m-> isFillable('nonexistent_field')); - > true

这是打破了我所有的测试。我如何强制雄辩尊重我设置的可填写规则,以便单元测试真正匹配现实?

解决方案

我正在运行也是这样。在单元测试中显然是Laravel的保镖模型,所以你必须用 Eloquent :: reguard()来调节它们。我会将它放在 setUp()方法中,以便每次测试之前运行。


Is there a reason why Eloquent ignores the fillable settings for a model when used in a unit test?

In a non test situation, I get the following output, which is correct, since the model only allows certain fillable fields:

$m = new Model();
dd($m->isFillable('nonexistent_field')); --> false

But if I do the same thing in a unit test:

$m = new Model();
dd($m->isFillable('nonexistent_field')); --> true

This is breaking all of my tests. How can I force Eloquent to respect the fillable rules I set for it so the unit tests actually match reality?

解决方案

I was running into this as well. Apparently Laravel unguards models during unit tests so you have to reguard them with Eloquent::reguard(). I would put it in the setUp() method so it runs before every test.

这篇关于Laravel 5.1雄辩是单位测试中的填充()差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆