How can I assert that no route matches in a Rails integration test?

余生长醉 提交于 2019-12-03 07:44:19

There is a similar way for checking this in Rails 4 by asserting on the UrlGenerationError exception:

def test_no_routes_match_when_neither_foo_nor_bar_exist
  assert_raises(ActionController::UrlGenerationError) do
    get '/category/this-is-neither-a-foo-nor-a-bar'
  end
end
Josh Glover

I ended up doing this:

  def test_no_routes_match_when_neither_foo_nor_bar_exist
    assert_raises(ActionController::RoutingError) do
      assert_recognizes({}, '/category/this-is-neither-a-foo-nor-a-bar')
    end
  end

Slightly silly, but it gets the job done.

Note that this does not work with Rails 4. See the answer below for a Rails 4 solution.

By calling #recognize_path you wont receive a false. Instead you'll get an error, but then you found the clue you were looking for.

test "No routes match when neither_foo_nor_ bar exist" do
  begin 
    assert_not(Rails.application.routes.recognize_path(
        'category/this-is-neither-a-foo-nor-a-bar'))
  rescue ActionController::RoutingError => error
    assert error.message.start_with? "No route matches"
  end
end
Nuno Silva

Have you tried method_missing(selector, *args, &block)? Defined Here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!