如何断言 Rails 集成测试中没有路由匹配?

发布于 2024-10-14 11:58:13 字数 474 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白云不回头 2024-10-21 11:58:13

Rails 4 中,有一种类似的方法可以通过在 UrlGenerationError 异常上断言来检查这一点:

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

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
好倦 2024-10-21 11:58:13

我最终这样做了:

  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

有点傻,但它完成了工作。

请注意,这不适用于 Rails 4。请参阅下面的答案了解 Rails 4 解决方案。

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.

潇烟暮雨 2024-10-21 11:58:13

通过调用#recognize_path,您不会收到错误。相反,您会收到错误,但随后您找到了您正在寻找的线索。

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

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
演出会有结束 2024-10-21 11:58:13

您是否尝试过method_missing(selector, *args, &block)?
定义此处

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文