Skip to content

Instantly share code, notes, and snippets.

@stuliston
Created September 10, 2011 04:44
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save stuliston/1207931 to your computer and use it in GitHub Desktop.
Save stuliston/1207931 to your computer and use it in GitHub Desktop.
Testing HABTM with rspec and factory girl
require 'spec_helper'
describe "Artists to releases relationship" do
before(:all) do
@kanye = FactoryGirl.create(:artist, :name => 'Kanye West')
@jz = FactoryGirl.create(:artist, :name => 'Jay Z')
@watch_the_throne = FactoryGirl.create(:release, :name => 'Watch the Throne')
@dropout = FactoryGirl.create(:release, :name => 'The College Dropout')
end
it "should recognise when an artist has no releases" do
@kanye.releases.count.should == 0
end
it "should handle an artist with a release" do
@kanye.releases << @dropout
@kanye.releases.count.should == 1
end
# (testing 2-way fixup)
it "should automatically know a release's artist" do
@kanye.releases << @dropout
@dropout.artists.count.should == 1
end
it "should hanlde an artist collaboration" do
@kanye.releases << @watch_the_throne
@jz.releases << @watch_the_throne
@watch_the_throne.artists.count.should == 2
end
end
@harshav17
Copy link

I did a similar implementation. Except in my case artist = user and release = team.

Following is my method that is similar to your test at line 27.

it "should automatically know a player's teams" do
 @pucks.users << @harsha
 @holics.users << @harsha
 expect(@harsha.teams.size).to eql 2
end

The problem is that the test is failing with the following error.

Failure/Error: expect(@harsha.teams.size).to eql 2

       expected: 2
            got: 1

       (compared using eql?)
     # ./spec/models/teams_users_spec.rb:36:in `block (2 levels) in <top (required)>'

associations are working as expected through the rails console. Do you, by any chance, have ideas as to why this is happening?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment