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.
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.
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.
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.