【问题标题】:Rspec + shoulda test fails on accept_nested_attributes_for despite relationship尽管有关系,但 Rspec + shoulda 测试在 accept_nested_attributes_for 上失败
【发布时间】:2016-06-30 19:38:46
【问题描述】:

我正在为我的应用程序模型编写模型测试,accepts_nested_attributes_for(:user)。这是失败的测试:

describe UserApplication, "associations" do
  it { should belong_to(:user) }
  it { should accept_nested_attributes_for(:user) }
end

这是模型:

class UserApplication < ActiveRecord::Base
   #attr_accessible all the fields

   belongs_to :user
   accepts_nested_attributes_for :user
end

我将 rspec 与 shoulda-matchers 2.8 一起使用,根据我的研究,这应该一切正常。也没有春天或任何其他奇怪的东西。这是我看到的错误:

1) UserApplication association 
 Failure/Error: it { should accept_nested_attributes_for(:user) }
 NoMethodError:
   undefined method `accept_nested_attributes_for' for #<RSpec::Core::ExampleGroup::Nested_2:0x007fec5c641a40>
 # ./spec/models/user_application_spec.rb:25:in `block (2 levels) in <top (required)>'

我不确定是什么原因造成的。这可能是一些奇怪的宝石冲突恶作剧还是我遗漏了一些明显的东西?

【问题讨论】:

  • 您可以尝试将您的 should-matcher 升级到最新版本并重试吗?看不到该特定版本的文档
  • @AnezioCampos 将 shoulda-matcher 更新到 3.0 需要我将 activesupport 更新到 4.0,遗憾的是我目前无法这样做。我暂时被困在rails 3.2上。
  • 这个it { should accept_nested_attributes_for(:user) } 应该是it { should accepts_nested_attributes_for(:user) }(您在accepts 上缺少s...` `
  • @Hackerman 我认为可能是这样,并看到其他一些线程表明这一点,但不幸的是没有骰子。刚刚测试了它,得到了相同的结果。
  • 我会仔细检查您是否已加载并且您的版本是否正确。例如,请参阅stackoverflow.com/a/2954632/1008891

标签: ruby-on-rails rspec shoulda


【解决方案1】:

好的,所以我必须感谢 @PeterAlfvin 为我提供了工具来查看问题所在。我的 gemfile 指定了gem 'shoulda-matchers',但这只是加载了不支持accepts_nested_attributes_for 的1.0 版。他通过https://stackoverflow.com/a/2954632/1008891 提出的建议为我指明了正确的方向。指定 2.8.0 解决了问题而不会立即破坏任何东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多