rails
(12,392 views)

Rspec example for nested resource index action

A nice clean example, posted by Martin Emde on rspec-users list
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

Here's the spec:

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

Comments

by Jesper Laursen on Oct 01, 2007

>>

It could be nice with some more examples. E.g. new, create, delete and edit.

by Prodis on Aug 21, 2009

Thanks

Thank you for the information.

by assemaz8h81 on Aug 31, 2016

xkih1pl0

http://nolvadex2.us/ - nolvadex http://antabuse3.us/ - buy cheap antabuse

by assemxizzqf on Sep 09, 2016

h3zg639o

http://buyclonidine.info/ - clonidine online http://buyfluoxetine.info/ - BUY FLUOXETINE http://azithromycin2016.us/ - azithromycin 500mg treatment

by asseminh0q0 on Sep 10, 2016

p3gkubn0

http://slotsite.top/ - roulette bonus http://howtomakemoneyonlinefast.top/ - how to make money selling t shirts online http://nointerestpaydayloans.top/ - payday loan no interest http://howtomakemoneyingtaonline.top/ - home based business in india http://paydayloansinalabama.top/ - payday loans in alabama http://casinonew.top/ - new casinos http://howtomakemoneyasaphotographer.top/ - how to make money in gta v

New Comment

markdown formatting permitted