How can I assert that no route matches in a Rails

2019-03-21 02:17发布

I have a Rails 3 integration test which tests my routes. It contains tests such as:

assert_routing(
    "/#{@category.url.path}/#{@foo.url.path}",
    { :controller => 'foo', :action => 'show', :category => @category.to_param, :foo => @foo.to_param }
)

I would also like to test a case where no routes should match. Obviously, testing generation has no meaning in this case, so I just need the inverse of assert_recognizes. I'd like to be able to do something like this:

assert_not_recognized('/adfhkljkdhasjklhjkldfahsjkhdf')

Any ideas, short of wrapping assert_recognizes in an assert_raises block (which is actually not so terrible, now that I think about it)?

4条回答
forever°为你锁心
2楼-- · 2019-03-21 02:53

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
查看更多
做个烂人
3楼-- · 2019-03-21 03:01

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

查看更多
ら.Afraid
4楼-- · 2019-03-21 03:02

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
查看更多
Luminary・发光体
5楼-- · 2019-03-21 03:07

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.

查看更多
登录 后发表回答