[rspec-users] Could anyone please help with rspec/nested resource behavior checking?

Martin Emde martin.emde at gmail.com
Sun Jun 3 15:07:21 EDT 2007


Here is the correct spec for this index action:

def index
if params[:user_id]
@user = User.find(params[:user_id])
@messages = @user.messages
end
end

describe MessagesController, " handling GET /messages for a user" do
before do
   @user = mock_model(User)
   @messages = mock("messages")
   @user.stub!(:messages).and_return(@messages)
   User.stub!(:find).and_return(@user)
end

def do_get
   get :index, :user_id => 1
end

it "should render index template" do
   do_get
   response.should render_template('index')
end

it "should find user with params[:user_id]" do
   User.should_receive(:find).with(1).and_return(@user)
   do_get
end

it "should get user's messages" do
   @user.should_receive(:messages).and_return(@messages)
   do_get
end

it "should assign the found messages for the view" do
   do_get
   assigns[:messages].should be(@messages)
end
end


Hope that helps you out.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070603/011115a7/attachment.html 


More information about the rspec-users mailing list