如何使用Minitest测试Rails路由?

时间:2012-07-08 04:49:38

标签: ruby-on-rails routing minitest

我正在尝试将Minitest用于现有的Rails应用程序(3.2),但没有运气路由测试。我已经尝试过rspec语法(应该是route_to)和TestUnit语法(assert_routing),但没有运气。

有关使此工作的任何建议吗?我需要包含的具体模块等?

感谢

2 个答案:

答案 0 :(得分:14)

如果您使用的是minitest-rails,可以通过在test/routes/homepage_test.rb中添加以下内容来创建路线测试:

require "minitest_helper"

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

或者,您可以使用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

您可以使用以下rake任务运行这些测试:

rake minitest:routes

答案 1 :(得分:2)

@ blowmage的答案对我有所帮助,但看起来语法有所改变。

使用Rails 4:

require "test_helper"

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