簡體   English   中英

Rspec:如何測試Rails的url_for助手?

[英]Rspec: how to test Rails' url_for helper?

使用RSpec,如何測試url_for輔助方法以確保它使用特定格式轉到正確的控制器和操作?

我必須做以下事情 -

將其添加到spec_helper.rb:

Rspec.configure do |config|
  config.include Rails.application.routes.url_helpers  # url_for
end

然后:

describe "Article routing" do
  it "routes to #index.atom" do
    get('/articles.atom').
      should route_to('articles#index', format: 'atom')

    url_for(:controller => 'articles', :format => :atom, :only_path => true).
      should == '/articles/index.atom'
  end
end

您可以測試命名路由是否路由到正確的位置。 路由規范存在於spec/routing ,因此以下規范將添加到spec/routing/widget_routes_spec.rb

describe "routes to the widgets controller" do
  it "routes a named route" do
    expect(:get => new_widget_path).
      to route_to(:controller => "widgets", :action => "new")
  end
end

路由規范文檔中描述的測試路徑還有很多其他選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM