Hey everyone. I really stuck on testing a nested controller. I''m trying to make a request using get and afterwards checking the response by response.should ... My routes.rb looks like this: map.resources :writers do |writers| writers.resources :notes end In my notes_controller_spec.rb def do_get writer_id = 1 note_id = 1 get note_path(writer_id, note_id) end it "should show a note" do do_get response.should be_success end But this always ends in an error message: You have a nil object when you didn''t expect it! The error occurred while evaluating nil.rewrite Can anybody help here?? -- by(e) Andreas Wolff
Ok. this was stupid :) I found the answer two minutes later: You cannot use the url helper here, but if you provide the writers id in the request everything works fine: testing the route: route_for(:controller => "notes", :action => "show", :id => 1, :writer_id => 1) testing the request by: get :show, :id => 1, :writer_id => 1 bye On 9/14/07, Andreas Wolff <rubyphunk at googlemail.com> wrote:> Hey everyone. > I really stuck on testing a nested controller. I''m trying to make a > request using get and afterwards checking the response by > response.should ... > > My routes.rb looks like this: > > map.resources :writers do |writers| > writers.resources :notes > end > > > In my notes_controller_spec.rb > > def do_get > writer_id = 1 > note_id = 1 > get note_path(writer_id, note_id) > end > > it "should show a note" do > do_get > response.should be_success > end > > > But this always ends in an error message: > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.rewrite > > Can anybody help here?? > > -- > by(e) > Andreas Wolff >-- by(e) Andreas Wolff http://rubyblog.de | http://activerails.de
On 14.9.2007, at 12.02, Andreas Wolff wrote:> Hey everyone. > I really stuck on testing a nested controller. I''m trying to make a > request using get and afterwards checking the response by > response.should ... > > My routes.rb looks like this: > > map.resources :writers do |writers| > writers.resources :notes > end > > > In my notes_controller_spec.rb > > def do_get > writer_id = 1 > note_id = 1 > get note_path(writer_id, note_id) > end > > it "should show a note" do > do_get > response.should be_success > end > > > But this always ends in an error message: > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.rewrite > > Can anybody help here??You shouldn''t test the routing stack in controller tests. Just say get :show, :id => 1, :writer_id => 1 It will automatically use the correct controller, given that you have either used "describe NotesController" or use the controller helper to identify the controller. I think the former way is now considered best practice. If you want to test routes, do it separately. IIRC generating an rspec_scaffold will create you examples of how to do that. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2417 bytes Desc: not available Url : http://rubyforge.org/pipermail/rspec-users/attachments/20070914/46f61372/attachment.bin