【发布时间】: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