0

I've got a Rails 3 application, and am trying to write tests for all the routing.

Imagine this very simple route:

resources :jobs, :constraints => {:id => /\d+/}

I now wish to test this. In my functional test, it's easy enough to write an assertion using the built-in assertions in Rails for the positive case:

assert_routing "jobs/4", { :controller => "jobs", :action => "show", :id => "4" }

What is the best way of asserting the negative? Namely that "jobs/wibble", having failed the constraint, is not being routed but rather returning an error? Is there some easy way of implementing "assert_not_routing" or something like that? Has anyone handled this before?

Tom Morris
  • 3,979
  • 2
  • 25
  • 43

2 Answers2

1

The solution I found in the end is to test routing of valid URLs in the functional tests and to test the non-routing of invalid URLs in integration tests.

I created a new integration test class called RoutesTest and in there store all the URLs that ought not route anywhere.

require 'test_helper'

class RoutesTest < ActionDispatch::IntegrationTest
  test "broken routes don't work" do
    assert_raise(ActionController::RoutingError) {
      get "/jobs/wibble"
    } 
  end
end

I still test the valid routes in the functional tests. There's some testing theology fail here, most likely: in the arbitrary and rather ridiculous theories we come up with about where particular tests should go in the unit vs. integration vs. functionals idea, it seems a bit strange. But I'd rather get the job done than argue testing theology with people. So this is how I'm doing it.

Tom Morris
  • 3,979
  • 2
  • 25
  • 43
  • Tom - is there a testing theology reason you're not using rspec? https://www.relishapp.com/rspec/rspec-rails/v/2-4/docs/routing-specs/be-routable-matcher. Or is this not doing what you're after? (should_not be_routable) – Chris S Jul 04 '12 at 10:12
  • The primary reason I'm not using RSpec is laziness, and also bad experiences in the past with Cucumber causing havoc with my Rails 2.x application. I probably ought to switch over to RSpec. – Tom Morris Jul 04 '12 at 10:17
  • well then I'll vote up your answer as it's "correct" and also now comes with an added juicy addendum to "just probably use rspec"! – Chris S Jul 04 '12 at 11:00
0

Have you tried asserting a nil route for the path?

Pius Uzamere
  • 1,414
  • 12
  • 10