【问题标题】:Rails view specs: referenced partials of inherited controllers aren't foundRails 视图规范:未找到继承控制器的引用部分
【发布时间】:2017-01-20 11:00:57
【问题描述】:

我有以下控制器:

ReportsController # Abstract
ReportTemplatesController < ReportsController
ReportDocumentsController < ReportsController

因此我有以下视图文件夹:

app/views/reports # Shared templates
app/views/report_templates
app/views/report_documents

例如,在.../report_templates/index.html.slim.../report_documents/index.html.slim 中,我都有这个调用:

== render 'reports', reports: @reports

_reports.html.slim 部分存在于.../reports,因为它被他们俩使用。这非常有效。

_reports.html.slim部分,我做了一些截断:

= truncate report.description, length: 100

我想使用视图规范来声明这一点,因为它比功能规范便宜。

require 'rails_helper'

RSpec.describe "report_templates/index", type: :view do
  it 'Truncates the description' do
    assign :reports, [create(:report_template, description: 'This is a very long description. This is a very long description. This is a very long description. This is a very long description. This is a very long description.')]
    render

    expect(rendered).to have_css '.description', text: 'This is a very long description. This is a very long description. This is a very long description...'
  end
end

不幸的是,这会产生以下错误:

Failures:

  1) reports/index Truncates the description
     Failure/Error: render

     ActionView::MissingTemplate:
       Missing template reports/index with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :slim, :coffee, :axlsx, :jbuilder, :haml]}. Searched in:
         * "/Users/josh/Documents/Work/Access4All/Projects/a4aa2/src/app/views"

但是当我换行时

RSpec.describe "reports/index", type: :view do

例如

RSpec.describe "reports_templates/index", type: :view do

错误变为:

Failures:

  1) report_templates/index Truncates the description
     Failure/Error: == render 'reports', reports: @reports

     ActionView::Template::Error:
       Missing partial /_reports, report_templates/_reports with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :slim, :coffee, :axlsx, :jbuilder, :haml]}. Searched in:
         * "/Users/josh/Documents/Work/Access4All/Projects/a4aa2/src/app/views"

因此,现在已找不到位于 reports 中的部分引用。

因此,无论我提供什么视图路径,规范似乎都不了解控制器继承并查看继承的路径本身。

我怎样才能使这个规范起作用?

【问题讨论】:

    标签: ruby-on-rails inheritance view rspec controller


    【解决方案1】:

    我在这里找到了有关该问题的更多信息:

    我通过这些链接找到了解决问题的一种临时方法:

    只需手动将继承的文件夹添加到查找路径:

    require 'rails_helper'
    
    RSpec.configure do |config|
      config.before(:example, type: :view) do
        view.lookup_context.prefixes << 'reports'
      end
    end
    
    RSpec.describe "report_templates/index", type: :view do
      # ...
    

    但这感觉很笨拙。没有办法自动执行此操作吗?

    我也不确定这是添加每个视图规范的路径(不应该)还是只添加这个(应该)。

    更新

    似乎当我将视图文件夹添加到渲染调用时,它可以在没有上述解决方法的情况下工作:

    == render 'reports/reports', reports: @reports
    

    而不仅仅是

    == render 'reports', reports: @reports
    

    我仍然觉得这并不干燥。

    更新

    您最好更改单个规范的查找上下文,否则您可能会弄乱其他视图规范(正如我在上面担心的那样),因为您在使用 RSpec.configure 时更改了每个规范的查找上下文。

    只需将此添加到特定的视图规范中:

    before do
      view.lookup_context.prefixes << 'reports'
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 2014-01-26
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      相关资源
      最近更新 更多