HI All, I have a controller with the following in it: def index @purchase_requests = PurchaseRequest.find(:all) @num_found = @purchase_requests.size end I want to test that @num_found is getting the number of purchase requests. This is what I''ve tried: it "should count the number of purchase requests found" do @purchase_requests.stub!(:size).and_return 3 @num_found.should_equal(3) get :index end However, I Get This Error: NoMethodError in ''PurchaseRequestsController Get index should count the number of purchase requests found'' You have a nil object when you didn''t expect it! The error occurred while evaluating nil.should_equal ./spec/controllers/purchase_requests_controller_spec.rb:21: It seems that rspec wants me to stub the instance variable. If I do that then I don''t think I''d actually be testing anything because I''d create the stub in the test and verify that the stub I created is there. I want to make sure that the test will fail if I delete the line in the actual controller or change the name. Can someone show me how to do this? thanks in advance. Leo
Thanks, That works at least to the point where it is testing the variable. However, it is calling it NIL. How do I tell the test to make it be 3? In the controller I have: @num_found = @purchase_requests.size This is what I tried in the spec: it "should count the number of purchase requests found" do @purchase_requests.stub!(:size).and_return 3 get :index assigns(:num_requests).should == 3 end This is the error: ''PurchaseRequestsController Get index should count the number of purchase requests found'' FAILED expected: 3, got: nil (using ==) ./spec/controllers/purchase On Jul 15, 3:05?pm, Adam Anderson <adamanderso... at gmail.com> wrote:> you need to use the assigns method like so > > ?it "should count the number of purchase requests found" do > ? ?get :index > ? ?assigns(:num_found).should == 3 > ?end > > On Wed, Jul 15, 2009 at 2:29 PM, Leo <leogodin... at gmail.com> wrote: > > HI All, > > I have a controller with the following in it: > > > ?def index > > ? ?@purchase_requests = PurchaseRequest.find(:all) > > ? ?@num_found = @purchase_requests.size > > ?end > > > I want to test that @num_found is getting the number of purchase > > requests. > > > This is what I''ve tried: > > > ?it "should count the number of purchase requests found" do > > ? ?@purchase_requests.stub!(:size).and_return 3 > > ? ?@num_found.should_equal(3) > > ? ?get :index > > ?end > > > However, I Get This Error: > > > NoMethodError in ''PurchaseRequestsController Get index should count > > the number of purchase requests found'' > > You have a nil object when you didn''t expect it! > > The error occurred while evaluating nil.should_equal > > ./spec/controllers/purchase_requests_controller_spec.rb:21: > > > It seems that rspec wants me to stub the instance variable. ?If I do > > that then I don''t think I''d actually be testing anything because I''d > > create the stub in the test and verify that the stub I created is > > there. ?I want to make sure that the test will fail if I delete the > > line in the actual controller or change the name. ?Can someone show me > > how to do this? > > thanks in advance. > > Leo > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
On Fri, Jul 17, 2009 at 1:08 PM, Leo Godin<leogodin217 at gmail.com> wrote:> Thanks, > That works at least to the point where it is testing the variable. > However, it is calling it NIL. ?How do I tell the test to make it be > 3? > > In the controller I have: > ?@num_found = @purchase_requests.size > > This is what I tried in the spec: > > ?it "should count the number of purchase requests found" do > ? ?@purchase_requests.stub!(:size).and_return 3 > ? ?get :index > ? ?assigns(:num_requests).should == 3 > ?end > > This is the error: > > ''PurchaseRequestsController Get index should count the number of > purchase requests found'' FAILED > expected: 3, > ? ? got: nil (using ==) > ./spec/controllers/purchaseWell first off the instance variable that your controller method is setting is @num_found, not @num_requests. So that''t the first problem. Then the @purchase_requests IV will refer to a different object when you stub it than after it gets set in your method. So @num_requests is going to be set to the size* of the association returned by the expression @purchase_requests = PurchaseRequest.find(:all)>From your initial post.One tack to take is to stub the PurchaseRequest class itself: it "should count the number of purchase requests found" do purchase = mock("purchase_requests", :size => 3) PurchaseRequest.stub!(:find).and_return(3) get :index assigns(:num_found).should == 3 end * I prefer to use length rather than size in Ruby because size sometimes means the number of elements, and sometimes something else like the number of bytes, depending in the receiver object. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale