(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
I understand that you are saying that I shouldn't use instance variables in the spec files. I'm still new so I don't have all of the best practices (like variable scope) down yet so I appreciate you pointing that out. Can you explain (or give a link for) why it is bad to use an instance variable in a spec file?
I'm not so sure @mhartl is saying the instance variable is the "wrong" part in that line of code though. In section 10.1.2 he says that the problem is that the user_id attribute is accessible. As far as I can tell, he repeatedly uses instance variables in his spec files in the tutorial (for the micropost model and the user model).