如何测试路线不起作用?

时间:2015-02-03 12:49:25

标签: ruby-on-rails ruby-on-rails-4 tdd testng

如何否定以下测试?

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

我想测试路线/posts/1 不存在

2 个答案:

答案 0 :(得分:2)

这应该有效。这将测试以下路线是否存在(可能是过度杀伤)

$> 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

答案 1 :(得分:0)

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

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

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