# subject thus provides a decent syntax for testing method calls directly.
# Sometimes, though, you have methods you want to test almost purely based on
# what is passed into them, with few other concerns of state, for instance, when
# testing class methods. These specs may have a little shared set-up needed, but
# really the true object under test is the method call itself. In these cases it
# makes sense to set that as the subject, and this leads to an effective and
# succinct, if slightly distasteful, pattern for setting the method arguments via
# standard rspec ivar trickery.
describe ".first" do
before { Person.create_10_people }
subject { Person.first(@number_of_people) }
it "should return the specified number of people" do
@number_of_people = 5
subject.size.should == 5
end
it "should return a single person when so specified" do
@number_of_people = 1
subject.should be_a(Person)
end
it "should default to returning a single person" do
subject.should be_a(Person)
end
end
I found your post when I was trying to avoid instance variables to pass 'arguments' to the subject. I like your approach and I'm using it right now. That's clever. Thnx.
Thanks, I had forgotten about this. I would rework #3 to use let(), of course.