2

How can I negate the following test?

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

I want to test that the route /posts/1 does not exist.

wintermeyer
  • 8,178
  • 8
  • 39
  • 85

2 Answers2

2

This should work. This will test if the route for :new exists (might be overkill)

$> 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
wintermeyer
  • 8,178
  • 8
  • 39
  • 85
DannielR
  • 148
  • 1
  • 9
0

The only thing I can come up with is try the route and then assert an exception will be raised:

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

Or

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

But I don't like either of those to be honest.

wpp
  • 7,093
  • 4
  • 33
  • 65