繁体   English   中英

如何使用Minitest测试Rails路由?

[英]How can I test Rails Routes with Minitest?

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

有什么建议让这个工作? 我需要包含的具体模块等?

谢谢

如果您使用的是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

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

使用Rails 4:

require "test_helper"

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM