Testing associated models with rspec

Hi,

I was wondering what is the best way to test associated models with rspec. Particularly I have a projects model that has_many stories. When I go to the projects show action it also lists all the stories associated to that project. I cant seem to get a test to work. I have tried the following:

  it "populates an array of user stories associated to @project" do
  project = FactoryGirl.create(:project)
  user_story = FactoryGirl.create(:user_story, project_id: project.id)

  get :show, id: project
  expect(assigns(:user_stories)).to eq(user_story)
end

If anyone can point me in the right directions it would be very much appreciated.

You’ve haven’t said what the failure is, but I suspect it’s because you’re comparing against a single user_story instead of an array containing one story, i.e.: eq([user_story]).

If that isn’t the case, please share the controller code and the RSpec failure message.

Hi Andy,

Apologies for it being vague I was leaving for the day. What I want to test is that the story I create is part of the collection of associated stories when I visit the project.

Failure/Error: expect(assigns(:user_stories)).to eq(user_story)
   
   expected: #<UserStory id: 12, actor: "Mrs. Issac Bode", narrative: "nemo", goal: "necessitatibus", project_id: 110, created_at: "2015-01-21 11:07:08", updated_at: "2015-01-23 01:01:26">
        got: #<ActiveRecord::Associations::CollectionProxy [#<UserStory id: 12, actor: "Mrs. Issac Bode", narrative: "nemo", goal: "necessitatibus", project_id: 110, created_at: "2015-01-21 11:07:08", updated_at: "2015-01-23 01:01:26">]>
   
   (compared using ==)
   
   Diff:
   @@ -1,2 +1,2 @@
   -#<UserStory id: 12, actor: "Mrs. Issac Bode", narrative: "nemo", goal: "necessitatibus", project_id: 110, created_at: "2015-01-21 11:07:08", updated_at: "2015-01-23 01:01:26">
   +[#<UserStory id: 12, actor: "Mrs. Issac Bode", narrative: "nemo", goal: "necessitatibus", project_id: 110, created_at: "2015-01-21 11:07:08", updated_at: "2015-01-23 01:01:26">]
   
 # ./spec/controllers/projects_controller_spec.rb:36:in `block (3 levels) in <top (required)>'

The failure message confirms my suspicion. The Diff: section indicates that the spec expected a single UserStory, but it instead got an array containing a UserStory.

Hi Andy,

Thanks for your reply. What do you feel is the best way to test it? I am stuck at the moment.

solved it. used:

expect(assigns(:user_stories)).to include(user_story)

thanks for pointing it out Andy it got me in the right direction.

I would suggest using eq([user_story]) since you want to test that assigns(:user_stories) contains only that single story.

(Alternatively, if you’re on a recent version of RSpec, you could use the contain_exactly matcher).

I wasnt clear before I dont want to just match that exact story because a project can have many. I want to test that the created story is assigned to the array of stories for that particular project.

Then I would indeed use the include matcher