簡體   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