I am developing a Rails 3.2 application and want the user to be redirect to a custom error page at errors#index when he tries to find a non-existing db record.
This is my code to do this inside the application controller:
if Rails.env.production?
rescue_from Exception, with: :display_error
rescue_from ActiveRecord::RecordNotFound, with: :display_error
end
private
def display_error
render "errors/index", status: 404
end
This actually works fine in production mode. Now the question is, how do I test it beforehand? I have tried this:
it "leads to error page on unknown db record" do
Rails.stub(:env).and_return("production")
# Sign in
@valid_user = FactoryGirl.create(:valid_user)
visit new_user_session_path
fill_in "Email", with: @valid_user.email
fill_in "Password", with: @valid_user.password
click_button "Sign in"
# Trigger exception
visit physician_path id: "this is not an id"
assert_contain "You blew it!"
end
However, this leaves me with two errors:
undefined method 'development?' for "production":Stringonvisit new_user_session_pathCouldn't find Physician with id=this is not an idonvisit physician_path id: "this is not an id"
The first one seems to imply an error with mocking the production environment; the second one will hopefully go away once I have implemented the actual code.
How can I solve these two errors? Thanks!
It looks like
Rails.envstores anActiveSupport::StringEnquirerrather than a plain string: https://github.com/rails/rails/blob/3096629d297a77a9b64747a0ac2df6b2cbf47a68/railties/lib/rails.rb#L81It might be easier to just stub the
production?method: