Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 392
doublea Avatar asked Jul 08 '12 04:07

doublea


2 Answers

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
like image 192
blowmage Avatar answered Sep 24 '22 03:09

blowmage


@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
like image 43
Dustin Avatar answered Sep 23 '22 03:09

Dustin