Chandu80
2011-Jul-12 09:36 UTC
[rspec-users] How to write render expectation in Controller Spec?
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
David Chelimsky
2011-Jul-12 10:59 UTC
[rspec-users] How to write render expectation in Controller Spec?
On Jul 12, 2011, at 4:36 AM, Chandu80 wrote:> Hello All, > I am a rspec beginnerWelcome!> 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
Chandu80
2011-Jul-13 06:54 UTC
[rspec-users] How to write render expectation in Controller Spec?
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 On Jul 12, 3:59?pm, David Chelimsky <dchelim... at gmail.com> wrote:> 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 > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users