1

I want to validate that my url in my web application returns 404 Not found. I know it doesn't exist, but I need to confirm that via tests by Rspec. How? This doesn't work:

it "404" do
  get "/some_url_404"
  expect(response.status).to eq 404
end

The test itself fails with:

ActionController::RoutingError: No route matches [GET] "/some_url_404"

update

the answer hasn't been given yet.

Karim
  • 41
  • 2
  • 8
  • Possible duplicate of [check https status code ruby](http://stackoverflow.com/questions/12685599/check-https-status-code-ruby) – javaDeveloper Dec 28 '16 at 14:09
  • Ruby! http://stackoverflow.com/questions/5908017/check-if-url-exists-in-ruby –  Dec 28 '16 at 14:10

3 Answers3

0

The most appropriate place for it is request spec (spec/requests subfolder). There the above snippet should work as expected.

Igor Springer
  • 498
  • 5
  • 7
  • Making the test a request spec doesn't change the underlying default behavior, it will still throw an exception. – yed Mar 12 '18 at 20:35
0

To achieve this, you can create a route to match all paths not defined using this:

get '*path', to: "controller#action"

The controller and action above denote the controller and action you want all requests for routes not listed should go. Here is how the controller action could be

#controller/action

def action
  render(:file => "#{Rails.root}/public/404.html", status: 404)
end

Note

Make sure the route is placed at the bottom of your route. If not, all requests will end there.

ydaniju
  • 400
  • 4
  • 11
0

The actual answer was covered in the post here.

You need to change the following settings in test.rb:

consider_all_requests_local = false
config.action_dispatch.show_exceptions = true
yed
  • 322
  • 2
  • 5