Hello All,
I am a rspec beginner 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.?
Thanks & Regards
Chandrika
On Jul 12, 2011, at 4:36 AM, Chandu80 wrote:
Hello All,
I am a rspec beginner
Welcome!
#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 …’: Generated controller spec in rspec-rails-2.6 · GitHub. If that doesn’t
make sense, feel free to ask questions.
HTH,
David
Thanks David…I am using jRuby on Rails1.5.0 and following are the
details of gems:-
rails-2.3.5
rspec-1.3.0
rspec-rails-1.3.2
My script needs to test the create channel method based on a condition
given by checkChannel? which I have stubbed as true.If true it should
render the action createchannel.
As mentioned above I used response.should
render_template(“createchannel”).It gave me the following error
NoMethodError in ‘ChannelsController should re-direct to create
channel’
undefined method `response’ for
#ActiveSupport::TestCase::Subclass_1:0x14b2263
When I removed all statements and kept only post :createchannel in the
if block,it gave me
NoMethodError in ‘ChannelsController should render the
createchannelpage’
undefined method `post’ for
#ActiveSupport::TestCase::Subclass_1:0x1f5205c
Is there any gem that needs to be installed that would make this work
and if yes is it compatible with jRuby on Rails?
Regards
Chandrika