doublea
doublea

Reputation: 2576

How can I test Rails Routes with Minitest?

I'm trying to use Minitest for an existing Rails app (3.2), but not having any luck running routing tests. I've tried rspec syntax (should route_to) and TestUnit syntax (assert_routing) but no luck.

Any advice on getting this to work? Specific modules I need to include, etc?

thanks

Upvotes: 5

Views: 4792

Answers (2)

blowmage
blowmage

Reputation: 8984

If you are using minitest-rails you can create route tests by placing the following in test/routes/homepage_test.rb:

require "minitest_helper"

class HomepageRouteTest < ActionDispatch::IntegrationTest
  def test_homepage
    assert_routing "/", :controller => "home", :action => "index"
  end
end

Alternatively, you can use the Minitest Spec DSL:

require "minitest_helper"

describe "Homepage Route Acceptance Test" do
  it "resolves the homepage" do
    assert_routing "/", :controller => "home", :action => "index"
  end
end

You can run these tests with the following rake task:

rake minitest:routes

Upvotes: 16

Dustin
Dustin

Reputation: 1026

@blowmage's answer helped me but it looks like the syntax has changed a bit.

With Rails 4:

require "test_helper"

class HomepageRouteTest < ActionDispatch::IntegrationTest
  def test_messages
    assert_generates '/messages', :controller => "messages", :action => "index"
  end
end

Upvotes: 3

Related Questions