Laravel 5.1 Eloquent isFillable() 单元测试中的差异 [英] Laravel 5.1 Eloquent isFillable() discrepancy in unit tests

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

问题描述

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屋!

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