class ErrorsController < ApplicationController
def not_found
respond_to do |format|
format.html { render status: 404 }
format.all { render nothing: true, status: 404 }
end
end
#..
end
# in developmen.rb I have below to test this behaviour
# config.consider_all_requests_local = false
# Now when I am hiting this url: http://localhost:3000/users/55 getting the log
And here goes my spec :
require 'rails_helper'
describe ErrorsController, type: :controller do
let(:user) { create :user }
let(:login_with) { user }
login_user
before :each do
Rails.application.config.consider_all_requests_local = false
Rails.application.config.action_dispatch.show_exceptions = true
end
after :each do
Rails.application.config.consider_all_requests_local = true
Rails.application.config.action_dispatch.show_exceptions = false
end
describe 'GET #not_found' do
it 'responds with 404 page' do
@controller = UsersController.new
user = create(:user)
get :show, id: user.to_param.next
expect(response).to have_http_status(404)
end
end
end
# now if you see the test log, the error page is not getting rendered.
Processing by UsersController#show as HTML
Parameters: {"id"=>"3"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND "users"."id" = $1 LIMIT 1 [["id", 3]]
Completed 404 Not Found in 5ms (ActiveRecord: 0.9ms)
(0.2ms) ROLLBACK
----
$ rspec spec/controllers/errors_controller_spec.rb
F
Failures:
1) ErrorsController GET #not_found responds with 404 page
Failure/Error: @user = User.find(params[:id])
ActiveRecord::RecordNotFound:
Couldn't find User with 'id'=3 [WHERE "users"."deleted_at" IS NULL]
# ./app/controllers/users_controller.rb:25:in `show'
# /Users/arup/.rvm/gems/ruby-2.2.2@teradial/gems/devise-3.5.2/lib/devise/test_helpers.rb:19:in `block in process'
# /Users/arup/.rvm/gems/ruby-2.2.2@teradial/gems/devise-3.5.2/lib/devise/test_helpers.rb:72:in `catch'
# /Users/arup/.rvm/gems/ruby-2.2.2@teradial/gems/devise-3.5.2/lib/devise/test_helpers.rb:72:in `_catch_warden'
# /Users/arup/.rvm/gems/ruby-2.2.2@teradial/gems/devise-3.5.2/lib/devise/test_helpers.rb:19:in `process'
# ./spec/controllers/errors_controller_spec.rb:23:in `block (3 levels) in <top (required)>'
Finished in 0.14492 seconds (files took 1.79 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/controllers/errors_controller_spec.rb:19 # ErrorsController GET #not_found responds with 404 page
Now, if when I hit the same non exiting route in development.rb, all works as expected :
Started GET "/users/55" for ::1 at 2015-12-20 21:06:12 +0530
Processing by UsersController#show as HTML
Parameters: {"id"=>"55"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND "users"."id" = $1 LIMIT 1 [["id", 55]]
Completed 404 Not Found in 3ms (ActiveRecord: 0.8ms)
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=55 [WHERE "users"."deleted_at" IS NULL]):
app/controllers/users_controller.rb:25:in `show'
Processing by ErrorsController#not_found as HTML
Parameters: {"id"=>"55"}
Rendered errors/not_found.html.erb within layouts/errors (0.0ms)
Completed 404 Not Found in 141ms (Views: 141.0ms | ActiveRecord: 0.0ms)
----
What configuration I am missing from Rspec point of view ?