如何断定Rails集成测试中没有路由匹配? [英] How can I assert that no route matches in a Rails integration test?
问题描述
我有一个Rails 3集成测试,可以测试我的路线.它包含以下测试:
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 }
)
我还想测试一种路由不匹配的情况.显然,在这种情况下,测试生成没有任何意义,因此我只需要相反的assert_recognizes.我希望能够做这样的事情:
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')
除了将assert_recognizes包装在assert_raises块中之外,还有什么想法(实际上,考虑到我现在考虑的并不那么糟糕)?
Any ideas, short of wrapping assert_recognizes in an assert_raises block (which is actually not so terrible, now that I think about it)?
推荐答案
我最终这样做:
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.
请注意,此不能与Rails 4一起使用.请参阅下面的答案以了解Rails 4解决方案.
Note that this does not work with Rails 4. See the answer below for a Rails 4 solution.
这篇关于如何断定Rails集成测试中没有路由匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!