I have this in my controller spec file
it "should raise 404" do
business = FactoryGirl.build(:business)
expect{get :edit, :id => business}.to raise_error(ActiveRecord::RecordNotFound)
end
if I am right, build does not save to the database, so business should not exist, and my test should pass, but it does not.
I also tried a string as a value of “id”, but it still fails.
I have tried with this controller action:
def edit
if params[:id].to_i == 0
name = params[:id].to_s.titleize
@business = Business.find_by_name!(name)
else
@business = Business.find(params[:id])
end
respond_with(@business)
end
an ID that does not exist, and it does indeed show a 404.
If you ask why a condition like that, I also make this action respond to a string for the “id” param.
Any ActiveRecord::RecordNotFound is received by this code in the application controller:
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
private
def record_not_found
render :text => "404 Not Found Baby!", :status => 404
end
why is my test for a 404 not passing?
Your controller does not raise
ActiveRecord::RecordNotFoundexception, it rescues from it in ApplicationController. So try to test for response code or text, something like