如何测试路由不工作?

How to test that a route does not work?

如何否定下面的测试?

test "should route to post" do
  post = posts(:one)
  assert_routing "/posts/#{post.id}", { 
                                        controller: "posts", 
                                        action: "show", 
                                        id: "#{post.id}" 
                                      }
end

我想测试路由/posts/1 存在。

我唯一能想到的就是尝试路由,然后断言将引发异常:

test "should route to post" do
  post = posts(:one)
  assert_raises Minitest::Assertion do
    assert_routing "/posts/#{post.id}"
  end
end

assert_raises ActionController::UrlGenerationError do
  get "/users/1"
end

但老实说,我不喜欢其中任何一个。

这应该有效。这将测试 :new 的路由是否存在(可能有点矫枉过正)

$> route.defaults #outputs: {:controller=>"posts", :action=>"index"}

test "shouldn't have route new" do
   admin_routes = Rails.application.routes.routes.
                  select { |route| route.path.spec.to_s.starts_with? "/posts" }

   admin_routes.each do |route|
       assert_not route.defaults[:action].
                        include?('new'), "route :new is not allow to exist"
   end
end