如何断言 Rails 集成测试中没有路由匹配?
我有一个 Rails 3 集成测试来测试我的路线。它包含以下测试:
assert_routing(
"/#{@category.url.path}/#{@foo.url.path}",
{ :controller => 'foo', :action => 'show', :category => @category.to_param, :foo => @foo.to_param }
)
我还想测试没有路由匹配的情况。显然,在这种情况下测试生成没有意义,所以我只需要断言_识别的逆。我希望能够做这样的事情:
assert_not_recognized('/adfhkljkdhasjklhjkldfahsjkhdf')
有什么想法,除了将assert_recognizes包装在assert_raises块中之外(这实际上并没有那么可怕,现在我想起来了)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Rails 4 中,有一种类似的方法可以通过在 UrlGenerationError 异常上断言来检查这一点:
There is a similar way for checking this in Rails 4 by asserting on the UrlGenerationError exception:
我最终这样做了:
有点傻,但它完成了工作。
请注意,这不适用于 Rails 4。请参阅下面的答案了解 Rails 4 解决方案。
I ended up doing this:
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.
通过调用#recognize_path,您不会收到错误。相反,您会收到错误,但随后您找到了您正在寻找的线索。
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.
您是否尝试过method_missing(selector, *args, &block)?
定义此处
Have you tried
method_missing(selector, *args, &block)
?Defined Here