Skip to content

Instantly share code, notes, and snippets.

@mehulkar
Created May 31, 2013 03:56
Show Gist options
  • Save mehulkar/5682867 to your computer and use it in GitHub Desktop.
Save mehulkar/5682867 to your computer and use it in GitHub Desktop.
# Lines 16-17 and 21-22 don't make sense to me.
# If I stub a method on the user object,
# why is it not stubeed for the same object on the association?
describe "#approved" do
let(:user) { FactoryGirl.create(:user) }
subject(:bulletin) { FactoryGirl.build(:bulletin_post) }
it "is false if the user is not approved" do
# user.stub(:approved?) { true }
bulletin.user_id = user.id
# bulletin.user === user
# => true
# user.approved?
# => true
# bulletin.user.approved?
# => false
bulletin.user.stub(:approved?) { true }
# user.approved?
# => true
# bulletin.user.approved?
# => true
expect(bulletin.approved?).to eq(false)
end
end
@dchelimsky
Copy link

why is it not stubbed for the same object on the association?

It's not the same object. On line 11, the bulletin gets a user_id, but then has to load the rest of the user data and a new object is created to house that data.

@icco
Copy link

icco commented May 31, 2013

Agreed. Also, another way to think of it is you haven't defined what bulletin.user will do. Just what the instance of user will do.

A better question is why are you testing something you are stubbing? Arguably it'd be a better test if you found the basis for approved?, spec'd that, and then made sure approved? changed.

(Warning, I don't know factorygirl or rspec, just kind of a classic mocking problem).

@mehulkar
Copy link
Author

@dchelimsky @icco sorry I didn't get notified of your responses.

So in line 11, if I were to assign bulletin.user = user instead of user_id, it should be the same data?

@icco, this test is testing that if the user who posted the Bulletin is not approved, the bulletin should not be approved either. The approved? property is on both models.

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