Laravel 5.1 Eloquent isFillable() 在单元测试中的差异 [英] Laravel 5.1 Eloquent isFillable() discrepancy in unit tests
问题描述
Eloquent 在单元测试中使用时忽略模型的可填充设置是否有原因?
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
这破坏了我所有的测试.如何强制 Eloquent 遵守我为其设置的可填充规则,以便单元测试真正符合现实?
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?
推荐答案
我也遇到了这个问题.显然,Laravel 在单元测试期间解除了模型的保护,因此您必须使用 Eloquent::reguard()
重新保护它们.我会将它放在 setUp()
方法中,以便它在每次测试之前运行.
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 Eloquent isFillable() 在单元测试中的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!