【发布时间】: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