Deepak Jois
2007-Jan-30 08:45 UTC
[rspec-users] errors while testing resource controller using rpec
I am testing a resource called venue in this piece of code (generated using script/rspec_resource) ===================context "Requesting /venues using POST" do controller_name :venues setup do @mock_venue = mock(''Venue'') @mock_venue.stub!(:save).and_return(true) @mock_venue.stub!(:to_param).and_return(1) Venue.stub!(:new).and_return(@mock_venue) end def do_post post :create, :venue => {:name => ''Venue''} end specify "should create a new venue" do Venue.should_receive(:new).with({''name'' => ''Venue''}).and_return(@mock_venue) do_post end end ========================= and the test fails as ==============TypeError in ''Requesting /venues using POST should create a new venue'' can''t convert Fixnum into String (eval):19:in `venue_url'' /home/deepak/personalcode/worlds/config/../app/controllers/venues_controller.rb:43:in `create'' /home/deepak/personalcode/worlds/config/../app/controllers/venues_controller.rb:40:in `create'' ./spec/controllers/venues_controller_spec.rb:242:in `do_post'' ./spec/controllers/venues_controller_spec.rb:247: =============== the relevant portion of the controller code is =============== # POST /venues # POST /venues.xml def create @venue = Venue.new(params[:venue]) respond_to do |format| if @venue.save flash[:notice] = ''Venue was successfully created.'' format.html { redirect_to venue_url(@venue) } format.xml { head :created, :location => venue_url(@venue) } else format.html { render :action => "new" } format.xml { render :xml => @venue.errors.to_xml } end end end ============= however, If I change the mock stub to return a string, the spec passes fine. @mock_venue.stub!(:to_param).and_return("1") # note the string There is something happening in the generated code venue_url which I am not aware of, which is causing the test to fail on the mock object. Is this a bug in the generated code.. or am I doing something wrong? Thanks Deepak
Deepak Jois
2007-Jan-30 08:47 UTC
[rspec-users] errors while testing resource controller using rpec
On 1/30/07, Deepak Jois <deepak.jois at gmail.com> wrote:> > however, If I change the mock stub to return a string, the spec passes fine. > > @mock_venue.stub!(:to_param).and_return("1") # note the string > > There is something happening in the generated code venue_url which I > am not aware of, which is causing the test to fail on the mock object.According to http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001033 the to_param method sends a stringified ID. Hence this should be a bug. Thanks Deepak
David Chelimsky
2007-Jan-30 11:04 UTC
[rspec-users] errors while testing resource controller using rpec
On 1/30/07, Deepak Jois <deepak.jois at gmail.com> wrote:> On 1/30/07, Deepak Jois <deepak.jois at gmail.com> wrote: > > > > however, If I change the mock stub to return a string, the spec passes fine. > > > > @mock_venue.stub!(:to_param).and_return("1") # note the string > > > > There is something happening in the generated code venue_url which I > > am not aware of, which is causing the test to fail on the mock object. > > According to http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001033 > > the to_param method sends a stringified ID. Hence this should be a bug.Hence you should report this to the rspec tracker: http://rspec.rubyforge.org/contribute.html http://rubyforge.org/mail/?group_id=797 Cheers, David> > Thanks > Deepak > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Deepak Jois
2007-Jan-30 11:20 UTC
[rspec-users] errors while testing resource controller using rpec
On 1/30/07, David Chelimsky <dchelimsky at gmail.com> wrote:> > Hence you should report this to the rspec tracker: > > http://rspec.rubyforge.org/contribute.html > http://rubyforge.org/mail/?group_id=797 > > Cheers, > David >And thus arises my meta-question :).. Since I am quote new to this, I dont know how to submit a patch for this with a spec As far as I can see, it is a VERY trivial patch which involves adding quotes around a number in the generator templates. Should I just go ahead and do that? Or is there a spec that I can actually write for such a trivial patch, Thanks Deepak
David Chelimsky
2007-Jan-30 11:33 UTC
[rspec-users] errors while testing resource controller using rpec
On 1/30/07, Deepak Jois <deepak.jois at gmail.com> wrote:> On 1/30/07, David Chelimsky <dchelimsky at gmail.com> wrote: > > > > Hence you should report this to the rspec tracker: > > > > http://rspec.rubyforge.org/contribute.html > > http://rubyforge.org/mail/?group_id=797 > > > > Cheers, > > David > > > > And thus arises my meta-question :).. Since I am quote new to this, I > dont know how to submit a patch for this with a spec > > As far as I can see, it is a VERY trivial patch which involves adding > quotes around a number in the generator templates. Should I just go > ahead and do that? Or is there a spec that I can actually write for > such a trivial patch,I''ll put up better instructions re: patches in the next release of the docs. In the mean time, since you''ve diagnosed the problem what you''ve reported above would be just fine. Something along the lines of "mocking :to_param should return a Fixnum" as the title and then describe the situation. Go ahead and stick that in the Bugs tracker (rather than a patch). Cheers, David> > Thanks > Deepak > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Chris Anderson
2007-Jan-30 17:24 UTC
[rspec-users] errors while testing resource controller using rpec
I filed a ticket on this issue the other night. I didn''t realize that to_param ought to return a string, so the bug does seem to be with the generated specs. But it is interesting to note that the issue does not affect Rails prior to rev 6062. I''ve invalidated my old ticket and submitted a patch that fixes the issue. Here it is: http://rubyforge.org/tracker/?func=detail&aid=8316&group_id=797&atid=3151 On 1/30/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 1/30/07, Deepak Jois <deepak.jois at gmail.com> wrote: > > On 1/30/07, David Chelimsky <dchelimsky at gmail.com> wrote: > > > > > > Hence you should report this to the rspec tracker: > > > > > > http://rspec.rubyforge.org/contribute.html > > > http://rubyforge.org/mail/?group_id=797 > > > > > > Cheers, > > > David > > > > > > > And thus arises my meta-question :).. Since I am quote new to this, I > > dont know how to submit a patch for this with a spec > > > > As far as I can see, it is a VERY trivial patch which involves adding > > quotes around a number in the generator templates. Should I just go > > ahead and do that? Or is there a spec that I can actually write for > > such a trivial patch, > > I''ll put up better instructions re: patches in the next release of the docs. > > In the mean time, since you''ve diagnosed the problem what you''ve > reported above would be just fine. Something along the lines of > "mocking :to_param should return a Fixnum" as the title and then > describe the situation. Go ahead and stick that in the Bugs tracker > (rather than a patch). > > Cheers, > David > > > > > Thanks > > Deepak > > _______________________________________________ > > 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 >-- Chris Anderson http://jchris.mfdz.com