在 Rspec 和 Rails 2.3.x 中使用质量分配 Shoulda 匹配器

发布于 2024-12-13 14:54:51 字数 324 浏览 1 评论 0原文

Rspec 的 Shoulda-matchers 不支持 ActiveRecord 类的allow_mass_assignment_of。

作为一种解决方法,您是否认为以下代码是等效的?

it { should_not allow_mass_assignment_of :id }

it { MyClass.protected_attributes.include?("id").should be_true }

Shoulda-matchers for Rspec do not support allow_mass_assignment_of for ActiveRecord classes.

As a workaround, would you regard the following code as equivalent?

it { should_not allow_mass_assignment_of :id }

it { MyClass.protected_attributes.include?("id").should be_true }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖 2024-12-20 14:54:51

根据源代码,两者是等效的,但第一行应该可以正常工作。
您可以在 https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb

According to the source code, both are equivalent, but the first line should work correctly.
You can check it on https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb

本宫微胖 2024-12-20 14:54:51

我也遇到这个问题。以下测试:

it should allow_mass_assignment_of(:email)

失败并显示错误消息,

undefined method 'allow_mass_assignment_of' for #<Class:0x1044de9c8> (NoMethodError)

而以下测试成功(从而确定 Shoulda 存在):

it should validate_uniqueness_of(:email)

shoulda-matchers 版本 1.0.0,rspec(和所有相关 gems)版本 2.8.0

看起来不是这样,但这是否与此问题有关:https://github.com/mergulhao/shoulda-matchers/commit/3a75c6877c07fbf62e77b1fb0aa852138bd537d6

I too am experiencing this problem. The following test:

it should allow_mass_assignment_of(:email)

fails with the error message

undefined method 'allow_mass_assignment_of' for #<Class:0x1044de9c8> (NoMethodError)

while the following test succeeds (thus establishing that Shoulda is present):

it should validate_uniqueness_of(:email)

shoulda-matchers version 1.0.0, rspec (and all related gems) version 2.8.0

It doesn't seem like it, but could this somehow be related to this issue: https://github.com/mergulhao/shoulda-matchers/commit/3a75c6877c07fbf62e77b1fb0aa852138bd537d6

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文