Rails rspec视图与区域设置,没有路由匹配

时间:2013-06-24 01:25:55

标签: locale rspec-rails

此规范通过但在引入区域设置后失败(应用程序有效):

require 'spec_helper'
describe "products/show" do
  before do
    assign(:product, mock_model("Product", name: "Car", description: "petrol engine"))
  end
  it "renders name" do
    render
    expect(rendered).to match /Car/
  end
end

然后我在路由中添加一个范围以包含区域设置: ...

  scope "/:locale" do
    resources :products
    root :to => 'products#index'
  end

...

在应用程序控制器中我定义:

def self.default_url_options(options={})
    logger.debug "default_url_options is passed options: #{options.inspect}\n"
    I18n.locale = 'en'  # fixed for tests
    { :locale => I18n.locale}
end

在浏览器中,应用程序再次使用路径 / en / product / 1来呈现节目模板

但我上面的测试失败了:

  

1)产品/展示呈现名称        失败/错误:渲染        ::的ActionView ::模板错误:          没有路由匹配{:action =>“edit”,:controller =>“products”,:locale =>#}        './app/views/products/show.html.erb:14:in _app_views_products_show_html_erb__333746538_80999240' # ./spec/views/products/show.html.erb_spec.rb:10:in阻止(2级)'

为什么应用程序运行时测试失败?

如何通过?

1 个答案:

答案 0 :(得分:1)

此解决方案适用于我:

https://github.com/rspec/rspec-rails/issues/255复制 作者:https://github.com/oelmekki

class ActionDispatch::Routing::RouteSet
  def url_for_with_locale_fix(options)
    url_for_without_locale_fix({:locale => I18n.default_locale}.merge(options))
  end
  alias_method_chain :url_for, :locale_fix
end