Chris Habgood
2011-May-27 16:39 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
Never seen the error above before, code: describe "edit action" do it "edit action should render edit template" do food = Food.create(:name=>''mooo'') # Food.any_instance.stubs(:valid?).returns(true) get :edit, :id => food.id response.should render_template(:edit) end end -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110527/26a99e37/attachment.html>
Ken Egervari
2011-May-28 15:47 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
I just found it bothersome to use stubs when testing rails controllers. A far easier way to do this is to define what @food is in a before(:each) block and then use it in your tests: describe "GET edit" do it "should assign the requested food to @food" do Food.should_receive(:find).with("1").and_return(@food) get :edit, :id => "1" assigns(:food).should be(@food) end end This is still very fast, and it has the added benefit that you can use "render_views" at the top of the spec to test your routes, erb code, etc. -> which is REALLY valuable. Ken On Fri, May 27, 2011 at 12:39 PM, Chris Habgood <chabgood at gmail.com> wrote:> Never seen the error above before, code: > > describe "edit action" do > it "edit action should render edit template" do > food = Food.create(:name=>''mooo'') # > Food.any_instance.stubs(:valid?).returns(true) > get :edit, :id => food.id > response.should render_template(:edit) > end > end > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110528/6c4af809/attachment.html>
Ken Egervari
2011-May-28 15:50 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
You also want to make sure that @food has an id of some kind, or Rails complains. For the ''new'' action, you want to set @food.id to nil to make sure the form works for the new action. You might not think this is important, but it is. If you decide to move this controller into a namespace for example, you are introducing a potential problem where the new or edit form doesn''t work anymore. I have found it less trouble to actually test it to make sure Rails doesn''t throw some error about trying to find a ''show'' action when it''s not supposed to. Ken On Sat, May 28, 2011 at 11:47 AM, Ken Egervari <ken.egervari at gmail.com>wrote:> I just found it bothersome to use stubs when testing rails controllers. > > A far easier way to do this is to define what @food is in a before(:each) > block and then use it in your tests: > > describe "GET edit" do > it "should assign the requested food to @food" do > Food.should_receive(:find).with("1").and_return(@food) > > get :edit, :id => "1" > > assigns(:food).should be(@food) > end > end > > This is still very fast, and it has the added benefit that you can use > "render_views" at the top of the spec to test your routes, erb code, etc. -> > which is REALLY valuable. > > Ken > > > On Fri, May 27, 2011 at 12:39 PM, Chris Habgood <chabgood at gmail.com>wrote: > >> Never seen the error above before, code: >> >> describe "edit action" do >> it "edit action should render edit template" do >> food = Food.create(:name=>''mooo'') # >> Food.any_instance.stubs(:valid?).returns(true) >> get :edit, :id => food.id >> response.should render_template(:edit) >> end >> end >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110528/913f4e1f/attachment.html>
Chris Habgood
2011-May-31 18:38 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
Still getting the same error. ugggh. On Sat, May 28, 2011 at 10:50, Ken Egervari <ken.egervari at gmail.com> wrote:> You also want to make sure that @food has an id of some kind, or Rails > complains. For the ''new'' action, you want to set @food.id to nil to make > sure the form works for the new action. > > You might not think this is important, but it is. If you decide to move > this controller into a namespace for example, you are introducing a > potential problem where the new or edit form doesn''t work anymore. I have > found it less trouble to actually test it to make sure Rails doesn''t throw > some error about trying to find a ''show'' action when it''s not supposed to. > > Ken > > > > On Sat, May 28, 2011 at 11:47 AM, Ken Egervari <ken.egervari at gmail.com>wrote: > >> I just found it bothersome to use stubs when testing rails controllers. >> >> A far easier way to do this is to define what @food is in a before(:each) >> block and then use it in your tests: >> >> describe "GET edit" do >> it "should assign the requested food to @food" do >> Food.should_receive(:find).with("1").and_return(@food) >> >> get :edit, :id => "1" >> >> assigns(:food).should be(@food) >> end >> end >> >> This is still very fast, and it has the added benefit that you can use >> "render_views" at the top of the spec to test your routes, erb code, etc. -> >> which is REALLY valuable. >> >> Ken >> >> >> On Fri, May 27, 2011 at 12:39 PM, Chris Habgood <chabgood at gmail.com>wrote: >> >>> Never seen the error above before, code: >>> >>> describe "edit action" do >>> it "edit action should render edit template" do >>> food = Food.create(:name=>''mooo'') # >>> Food.any_instance.stubs(:valid?).returns(true) >>> get :edit, :id => food.id >>> response.should render_template(:edit) >>> end >>> end >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >> >> > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- *"In matters of style, swim with the current; in matters of principle, stand like a rock." Thomas Jefferson * -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110531/12f5df40/attachment.html>
Ken Egervari
2011-May-31 18:50 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
On Tue, May 31, 2011 at 2:38 PM, Chris Habgood <chabgood at gmail.com> wrote:> Still getting the same error. ugggh.Truly, you have to give us some more code to help you. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110531/210cc9a8/attachment-0001.html>
Chris Habgood
2011-May-31 18:57 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
The program works when I run it on the server. describe FoodsController do render_views before(:each) do Food.delete_all login_as_admin Food.stubs(:find).with("1").returns(@food = mock_model(Food, :save=>false)) end #describe "stub_model(Food) with a hash of stubs" do #let(:food) do # stub_model Food, :id => 5, :food =>{:name => ''brisket''} #end describe "GET edit" do it "should assign the requested food to @food" do #Food.should_receive(:find).with("1").and_return(@food) puts @food get :edit, :id => @food.id assigns(:food).should be(@food) end end end On Tue, May 31, 2011 at 13:50, Ken Egervari <ken.egervari at gmail.com> wrote:> > On Tue, May 31, 2011 at 2:38 PM, Chris Habgood <chabgood at gmail.com> wrote: > >> Still getting the same error. ugggh. > > > Truly, you have to give us some more code to help you. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- *"In matters of style, swim with the current; in matters of principle, stand like a rock." Thomas Jefferson * -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110531/05741538/attachment.html>
Chris Habgood
2011-May-31 19:15 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
Full Error: Failures: 1) FoodsController should assign the requested food to @food Failure/Error: get :edit, :id => @food.id ActionView::Template::Error: undefined method `model_name'' for NilClass:Class # ./app/views/foods/_form.html.erb:1:in `_app_views_foods__form_html_erb__2500079613968748019_2175296860__3835516174707068795'' # ./app/views/foods/edit.html.erb:3:in `_app_views_foods_edit_html_erb__1881942591214137850_2175310520__4134993942280106228'' # ./app/controllers/foods_controller.rb:30:in `edit'' # ./spec/controllers/foods_controller_spec.rb:21:in `block (2 levels) in <top (required)>'' On Tue, May 31, 2011 at 13:57, Chris Habgood <chabgood at gmail.com> wrote:> The program works when I run it on the server. > > describe FoodsController do > render_views > > before(:each) do > Food.delete_all > login_as_admin > Food.stubs(:find).with("1").returns(@food = mock_model(Food, > :save=>false)) > end > > #describe "stub_model(Food) with a hash of stubs" do > #let(:food) do > # stub_model Food, :id => 5, :food =>{:name => ''brisket''} > #end > > > describe "GET edit" do > it "should assign the requested food to @food" do > #Food.should_receive(:find).with("1").and_return(@food) > puts @food > > get :edit, :id => @food.id > assigns(:food).should be(@food) > end > end > end > On Tue, May 31, 2011 at 13:50, Ken Egervari <ken.egervari at gmail.com>wrote: > >> >> On Tue, May 31, 2011 at 2:38 PM, Chris Habgood <chabgood at gmail.com>wrote: >> >>> Still getting the same error. ugggh. >> >> >> Truly, you have to give us some more code to help you. >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > > > -- > *"In matters of style, swim with the current; in matters of principle, > stand like a rock." > Thomas Jefferson > * >-- *"In matters of style, swim with the current; in matters of principle, stand like a rock." Thomas Jefferson * -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110531/223e1c0a/attachment.html>
Ken Egervari
2011-May-31 19:33 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
On Tue, May 31, 2011 at 2:57 PM, Chris Habgood <chabgood at gmail.com> wrote:> The program works when I run it on the server. > > describe FoodsController do > render_views > > before(:each) do > Food.delete_all > login_as_admin > Food.stubs(:find).with("1").returns(@food = mock_model(Food, > :save=>false)) > end > > #describe "stub_model(Food) with a hash of stubs" do > #let(:food) do > # stub_model Food, :id => 5, :food =>{:name => ''brisket''} > #end > >In my earlier post, I''ve already told you not to use mocks for your models. It''s more hassle than it is worth. before(:each) do @food = Food.new @food.id = 1 end describe "GET ''edit''" do it "should be successful" do Food.stub(:find).with("1").and_return(@food) get :edit assigns(:food).should == @food end end ^ This should work.> > describe "GET edit" do > it "should assign the requested food to @food" do > #Food.should_receive(:find).with("1").and_return(@food) > puts @food > > get :edit, :id => @food.id > assigns(:food).should be(@food) > end > end > end > On Tue, May 31, 2011 at 13:50, Ken Egervari <ken.egervari at gmail.com>wrote: > >> >> On Tue, May 31, 2011 at 2:38 PM, Chris Habgood <chabgood at gmail.com>wrote: >> >>> Still getting the same error. ugggh. >> >> >> Truly, you have to give us some more code to help you. >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > > > -- > *"In matters of style, swim with the current; in matters of principle, > stand like a rock." > Thomas Jefferson > * > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110531/fe9bca7a/attachment.html>
Ken Egervari
2011-May-31 19:35 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
Oh, don''t forget the :id in the call to edit before(:each) do @food = Food.new @food.id = 1 end describe "GET ''edit''" do it "should be successful" do Food.stub(:find).with("1").and_return(@food) get :edit, :id => "1" assigns(:food).should == @food end end Ken -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110531/8dc2af4e/attachment-0001.html>
Chris Habgood
2011-May-31 20:03 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
Ya, that is not working. The code I gave you I was trying different things out. Rails 3.0 Rspec rspec (2.6.0.rc6) On Tue, May 31, 2011 at 14:35, Ken Egervari <ken.egervari at gmail.com> wrote:> Oh, don''t forget the :id in the call to edit > > > before(:each) do > @food = Food.new > @food.id = 1 > end > > describe "GET ''edit''" do > it "should be successful" do > Food.stub(:find).with("1").and_return(@food) > > get :edit, :id => "1" > > > assigns(:food).should == @food > end > end > > Ken > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- *"In matters of style, swim with the current; in matters of principle, stand like a rock." Thomas Jefferson * -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110531/5e50cacd/attachment.html>
Ken Egervari
2011-May-31 20:33 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
Dumb question, do you have required "spec_helper" at the top of the file? Ken On Tue, May 31, 2011 at 4:03 PM, Chris Habgood <chabgood at gmail.com> wrote:> Ya, that is not working. The code I gave you I was trying different things > out. > Rails 3.0 > Rspec rspec (2.6.0.rc6) > > On Tue, May 31, 2011 at 14:35, Ken Egervari <ken.egervari at gmail.com>wrote: > >> Oh, don''t forget the :id in the call to edit >> >> >> before(:each) do >> @food = Food.new >> @food.id = 1 >> end >> >> describe "GET ''edit''" do >> it "should be successful" do >> Food.stub(:find).with("1").and_return(@food) >> >> get :edit, :id => "1" >> >> >> assigns(:food).should == @food >> end >> end >> >> Ken >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > > > -- > *"In matters of style, swim with the current; in matters of principle, > stand like a rock." > Thomas Jefferson > * > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110531/5fe6d47b/attachment.html>
Chris Habgood
2011-May-31 20:59 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
yes On Tue, May 31, 2011 at 15:33, Ken Egervari <ken.egervari at gmail.com> wrote:> Dumb question, do you have required "spec_helper" at the top of the file? > > Ken > > > > On Tue, May 31, 2011 at 4:03 PM, Chris Habgood <chabgood at gmail.com> wrote: > >> Ya, that is not working. The code I gave you I was trying different >> things out. >> Rails 3.0 >> Rspec rspec (2.6.0.rc6) >> >> On Tue, May 31, 2011 at 14:35, Ken Egervari <ken.egervari at gmail.com>wrote: >> >>> Oh, don''t forget the :id in the call to edit >>> >>> >>> before(:each) do >>> @food = Food.new >>> @food.id = 1 >>> end >>> >>> describe "GET ''edit''" do >>> it "should be successful" do >>> Food.stub(:find).with("1").and_return(@food) >>> >>> get :edit, :id => "1" >>> >>> >>> assigns(:food).should == @food >>> end >>> end >>> >>> Ken >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >> >> >> >> -- >> *"In matters of style, swim with the current; in matters of principle, >> stand like a rock." >> Thomas Jefferson >> * >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- *"In matters of style, swim with the current; in matters of principle, stand like a rock." Thomas Jefferson * -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110531/93d051cc/attachment.html>
Phillip Koebbe
2011-May-31 22:01 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
On 2011-05-31 1:57 PM, Chris Habgood wrote:> The program works when I run it on the server. > > describe FoodsController do > render_views > > before(:each) do > Food.delete_all > login_as_admin > Food.stubs(:find).with("1").returns(@food = mock_model(Food, > :save=>false)) > end > > #describe "stub_model(Food) with a hash of stubs" do > #let(:food) do > # stub_model Food, :id => 5, :food =>{:name => ''brisket''} > #end > > describe "GET edit" do > it "should assign the requested food to @food" do > #Food.should_receive(:find).with("1").and_return(@food) > puts @food > get :edit, :id => @food.id <http://food.id>If this is how you are actually making the ''get :edit'' call, you might try converting your @food.id to a string. When the controller gets the params, everything is a string, but if you give RSpec an integer, it will obediently pass it along. get :edit, :id => @food.id.to_s I should point out at this point that I have not yet really used the RSpec 2.x family, so I might be mistaken. But with 1.x, this is the case. Peace, Phillip
Ken Egervari
2011-May-31 22:16 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
Can you give me the whole test code, with no comments? Just the stuff you are running. Thanks Ken On Tue, May 31, 2011 at 4:59 PM, Chris Habgood <chabgood at gmail.com> wrote:> yes > > > On Tue, May 31, 2011 at 15:33, Ken Egervari <ken.egervari at gmail.com>wrote: > >> Dumb question, do you have required "spec_helper" at the top of the file? >> >> Ken >> >> >> >> On Tue, May 31, 2011 at 4:03 PM, Chris Habgood <chabgood at gmail.com>wrote: >> >>> Ya, that is not working. The code I gave you I was trying different >>> things out. >>> Rails 3.0 >>> Rspec rspec (2.6.0.rc6) >>> >>> On Tue, May 31, 2011 at 14:35, Ken Egervari <ken.egervari at gmail.com>wrote: >>> >>>> Oh, don''t forget the :id in the call to edit >>>> >>>> >>>> before(:each) do >>>> @food = Food.new >>>> @food.id = 1 >>>> end >>>> >>>> describe "GET ''edit''" do >>>> it "should be successful" do >>>> Food.stub(:find).with("1").and_return(@food) >>>> >>>> get :edit, :id => "1" >>>> >>>> >>>> assigns(:food).should == @food >>>> end >>>> end >>>> >>>> Ken >>>> >>>> _______________________________________________ >>>> rspec-users mailing list >>>> rspec-users at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/rspec-users >>>> >>> >>> >>> >>> -- >>> *"In matters of style, swim with the current; in matters of principle, >>> stand like a rock." >>> Thomas Jefferson >>> * >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >> >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > > > -- > *"In matters of style, swim with the current; in matters of principle, > stand like a rock." > Thomas Jefferson > * > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110531/2bcf4b39/attachment-0001.html>
Ken Egervari
2011-May-31 22:44 UTC
[rspec-users] [rails] undefined method `model_name'' for NilClass:Class
> On 2011-05-31 1:57 PM, Chris Habgood wrote: > >> The program works when I run it on the server. >> >> describe FoodsController do >> render_views >> >> before(:each) do >> Food.delete_all >> login_as_admin >> Food.stubs(:find).with("1").returns(@food = mock_model(Food, >> :save=>false)) >> end >> >> #describe "stub_model(Food) with a hash of stubs" do >> #let(:food) do >> # stub_model Food, :id => 5, :food =>{:name => ''brisket''} >> #end >> >> describe "GET edit" do >> it "should assign the requested food to @food" do >> #Food.should_receive(:find).with("1").and_return(@food) >> puts @food >> get :edit, :id => @food.id <http://food.id> >> > > If this is how you are actually making the ''get :edit'' call, you might try > converting your @food.id to a string. When the controller gets the params, > everything is a string, but if you give RSpec an integer, it will obediently > pass it along. > > get :edit, :id => @food.id.to_s > > I should point out at this point that I have not yet really used the RSpec > 2.x family, so I might be mistaken. But with 1.x, this is the case. > > Peace, > Phillip > >I want to point that the @food.id =1 is not really part of the test, but is to make "render_views" work. If @food is placed into a path, such as edit_food_path(@food), the method will require that @food.id be set. By setting it at the beginning of the test, you help Rails out. As for the line that calls the controller action, it''s important to pass a string because this is what Rails would do. It will not convert it to an integer. In fact, find("1") will still work in the real world - Rails itself will convert "1" to 1 internally. This is why you don''t have to say, Food.find(params[:id].to_i) While it might be a bit confusing, the "1" and the @food.id = 1 are not related. Honestly, you can use id of 50 or whatever you wanted - it''s just best to make sure it''s not `nil`. However, there is one exception to this rule. When you are testing the controller''s `new` action, you SHOULD set @food.id = nil. Why? Because you want to test how the form_for in the _form.html.erb behaves given an object without an id. It needs to work properly given objects with and without ids. If you always set @food.id to some value, you won''t be testing the case where _form.html.erb is given a new object - and this can spell surprises in some cases. Ken -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110531/d186eb3c/attachment.html>