(this started on twitter)
The microposts spec starts out with
let(:user) { FactoryGirl.create(:user) }
before do
# This code is wrong!
@micropost = Micropost.new(content: "Lorem ipsum", user_id: user.id)
end
subject { @micropost }
Michael is telling you the code is wrong to set an instance variable in the spec. Instead, you should just:
let(:user) { FactoryGirl.create(:user) }
subject { Micropost.new(content: "Lorem ipsum", user_id: user.id) }
This way, the subject is set to the Micropost.new
The same happens in my Dog example... We are setting the subject to a new instance of Dog.
describe Dog do
This will set the subject
to Dog.new
That's not why the code is wrong. Using instance variables in specs is fine, although I prefer to use
let
when I can. The code is wrong because setting theuser_id
doesn't work as soon as you defineattr_accessible
. Instead, you need to define the micropost through the association, using code likeuser.microposts.build(...)
, as described in Section 10.1.3.