简体   繁体   中英

Rspec + shoulda test fails on accept_nested_attributes_for despite relationship

I'm writing a model test for my Applications model, which accepts_nested_attributes_for(:user) . Here's the test that's failing:

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

And here's the model:

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

   belongs_to :user
   accepts_nested_attributes_for :user
end

I'm using rspec with shoulda-matchers 2.8, and from my research this should all work fine. No spring or any other weirdness, either. Here's the error I'm seeing:

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)>'

I'm not sure what could be causing this. Could this be some weird gem conflict shenanigans or am I missing something obvious?

Okay, so I have to give credit to @PeterAlfvin for giving me the tools to see what was wrong. My gemfile specified gem 'shoulda-matchers' but that was only loading up version 1.0, which didn't have support for accepts_nested_attributes_for . His suggestion, via https://stackoverflow.com/a/2954632/1008891 pointed me in the right direction. Specifying 2.8.0 fixed the problem without immediately breaking anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM