Skip to content

Instantly share code, notes, and snippets.

@brandoncordell
Last active December 12, 2015 05:48
Show Gist options
  • Save brandoncordell/4724607 to your computer and use it in GitHub Desktop.
Save brandoncordell/4724607 to your computer and use it in GitHub Desktop.
Rails reporting 404, rspec is reporting 200
# spec/controllers/pages_controller_spec.rb
require 'spec_helper'
describe PagesController do
describe "#show" do
context "with a published slug" do
...
end
context "with an unpublished slug" do
it "returns 404" do
get :show, id: :unknown
response.code.should eq 404 # This is the trouble code
end
end
end
end
# app/controllers/pages_controller.rb
class PagesController < ApplicationController
rescue_from ActiveRecord::RecordNotFound, with: :page_not_found
def show
@page = Page.published.find_by_slug!(params[:id])
end
protected
def page_not_found
raise ActionController::RoutingError.new('Page Not Found')
end
end
=begin
RSpec output
PagesController
#show
with a published slug
returns http success
with an unpublished slug
returns 404 (FAILED - 1)
Failures:
1) PagesController#show with an unpublished slug returns 404
Failure/Error: get :show, id: :unknown
ActionController::RoutingError:
Page Not Found
# ./app/controllers/pages_controller.rb:11:in `page_not_found'
# ./spec/controllers/pages_controller_spec.rb:15:in `block (4 levels) in <top (required)>'
=end
=begin
# Rails logger output
Page Load (0.1ms) SELECT "pages".* FROM "pages" WHERE "pages"."published" = 't' AND "pages"."slug" = 'homes' LIMIT 1
Completed 404 Not Found in 4ms
ActiveRecord::RecordNotFound (Couldn't find Page with slug = homes):
app/controllers/pages_controller.rb:5:in `show'
Rendered /Users/brandon/.rvm/gems/ruby-1.9.3-head@closetcrunch/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.9ms)
Rendered /Users/brandon/.rvm/gems/ruby-1.9.3-head@closetcrunch/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.4ms)
Rendered /Users/brandon/.rvm/gems/ruby-1.9.3-head@closetcrunch/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (13.2ms)
=end
@lindevloeberghs
Copy link

Hi, did you find a solution to this issue? I have the opposite problem, i.e. rails reports 200 but rspec fails..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment