Rails视图规范:未找到继承控制器的引用部分

时间:2017-01-20 11:00:57

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

我有以下控制器:

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

所以现在找不到type Context struct中的部分参考文献了。

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

如何使此规范有效?

1 个答案:

答案 0 :(得分:3)

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

我通过以下链接找到了解决问题的临时解决方案:

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

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