[rspec-users] errors while testing resource controller using rpec
Deepak Jois
deepak.jois at gmail.com
Tue Jan 30 03:45:13 EST 2007
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
More information about the rspec-users
mailing list