On Jul 12, 2011, at 4:36 AM, Chandu80 wrote:
> Hello All,
> I am a rspec beginner
Welcome!
> and I am trying to find a way out to write
> render expectation in controller spec. I used the following ways
> render '/channels/createchannel.html.erb'
> view.should_receive(:render).with(:action => "createchannel")
> ChannelsController.expect_render(:action => "createchannel")
> ChannelsController.render_template(:action => "createchannel")
> controller.expect_render(:action => "createchannel")
> controller.render_template(:action => "createchannel")
>
> All fail giving a NoMethodError
>
> **********************************************************************************
> My spec is as follows
>
> require File.dirname(__FILE__) + '/../spec_helper'
>
> describe ChannelsController do
> it "should re-direct to create channel" do
> Channel.stub!(:checkChannel?).and_return(1)
> ChannelsController.expect_render(:action => "createchannel")
>
> #view.should_receive(:render).with(:action => "createchannel")
> #render '/channels/createchannel.html.erb'
> end
>
>
> end
> **********************************************************************************
>
>
> Can anyone please let me know how this can be fixed.?
Please always include the versions of Rails and RSpec that you are using when
asking for help. Things are different in different versions. I'm going to
assume you're using rails-3 and rspec-rails-2.
The name of the example begins with "should re-direct ...". The rails testing
framework lets you assert that a specific template is rendered or that there is
a redirect, but not both in the same action.
You also have to invoke an action on the controller in each example for
anything to happen. e.g.
describe "POST create" do
describe "with valid params" do
it "redirects to the created channel" do
post :create, :channel => valid_attributes
response.should redirect_to(Channel.last)
end
end
describe "with invalid params" do
it "re-renders the 'new' template" do
Channel.any_instance.stub(:save).and_return(false)
post :create, :channel => {}
response.should render_template("new")
end
end
end
Take a look at the generated controller spec when you run 'rails generate
scaffold ...': https://gist.github.com/942203. If that doesn't make sense, feel
free to ask questions.
HTH,
David
> Thanks & Regards
> Chandrika
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users