【问题标题】:Rails rspec view with locale, No route matches带有区域设置的 Rails rspec 视图,没有路线匹配
【发布时间】:2013-06-24 01:25:54
【问题描述】:

此规范通过但在引入语言环境后失败(但该应用程序有效):

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

在浏览器中,应用程序再次使用路径,如 /zh/product/1 渲染显示模板

但是我上面的测试失败了:

1) 产品/节目呈现名称 失败/错误:渲染 动作视图::模板::错误: 没有路线匹配 {: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:inblock (2 个级别) in '

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

如何让它通过?

【问题讨论】:

标签: locale rspec-rails


【解决方案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

【讨论】:

  • 'rspec spec/views' 现在可以工作,但 'spring rspec spec/views' 不能 ActionView::Template::Error: stack level too deep # /usr/local/rvm/gems/ ruby-1.9.3-head/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:23 我习惯于使用guard-spring进行测试。现在这不适用于视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
  • 2012-07-13
  • 2012-10-11
相关资源
最近更新 更多