"...[A]ll you need to do is write out (on paper/in Vim/whatever) the behaviour of each scenario that can occur while traversing through this controller. For example, one [verbose] scenario is:
"When a user who's not logged-in and whose session isn't being redirected requests HTML from FooBarsController#show , they should be redirected to the sign-up page."
"Once you have the behaviour of each scenario mapped out, write specs for each scenario.
"When that's all done, I recommend writing your specs before you write code. -Nick
Though you may need to also stub requires_user if it's in a before filter (which I think I remember from another thread on this), so you'd actually want:
This way requires_user() doesn't do anything, and the user() method returns the mock user. Of course, it's going to need to respond to the avatars() method:
describe Hero do
it "should describe its surroundings" do
hero = Hero.new(room)
room.stub!(:description).and_return("a room with twisty passages")
console.should_receive(:show).with("in a room with twisty passages")
hero.look(console)
end
end
Comments
http://rubyforge.org/pipermail/rspec-users/2009-May/014712.html
"...[A]ll you need to do is write out
(on paper/in Vim/whatever) the behaviour of each scenario that can
occur while traversing through this controller. For example, one
[verbose] scenario is:
"When a user who's not logged-in and whose session isn't being
redirected requests HTML from FooBarsController#show , they should be
redirected to the sign-up page."
"Once you have the behaviour of each scenario mapped out, write specs
for each scenario.
"When that's all done, I recommend writing your specs before you write code.
-Nick
That aside, the nil error is happening because the user() method is
returning nil (in user.avatars). You can fix that by changing this:
to this:
Though you may need to also stub requires_user if it's in a before
filter (which I think I remember from another thread on this), so
you'd actually want:
This way requires_user() doesn't do anything, and the user() method
returns the mock user. Of course, it's going to need to respond to the
avatars() method:
or something like that.
http://www.m3p.co.uk/blog/2009/03/08/mock-roles-not-objects-live-and-in-person/
describe Hero do it "should describe its surroundings" do hero = Hero.new(room) room.stub!(:description).and_return("a room with twisty passages") console.should_receive(:show).with("in a room with twisty passages") hero.look(console) end end