【发布时间】:2011-12-22 06:40:02
【问题描述】:
我正在运行 Rails 3.1.1、RSpec 2.7.0 和 HAML 3.1.3。
假设我有以下视图文件:
应用程序/视图/布局/application.html.haml!!!
%html
%head
%title Test
= stylesheet_link_tag "application"
= javascript_include_tag "application"
= csrf_meta_tags
%body
= content_for?(:content) ? yield(:content) : yield
应用程序/视图/布局/companies.html.haml
- content_for :content do
#main
= yield :main
#sidebar
= yield :sidebar
= render :template => 'layouts/application'
应用程序/视图/公司/index.html.haml
- content_for :main do
%h1 MainHeader
- content_for :sidebar do
%h1 SidebarHeader
还有以下规范文件:
规范/视图/公司/index_spec.rbrequire 'spec_helper'
describe 'companies/index.html.haml' do
it 'should show the headers' do
render
rendered.should contain('MainHeader')
rendered.should contain('SidebarHeader')
end
end
当我运行 RSpec 时,我收到以下错误:
1) companies/index.html.haml should show the headers
Failure/Error: rendered.should contain('MainHeader')
expected the following element's content to include "MainHeader":
# ./spec/views/companies/index_spec.rb:7:in `block (2 levels) in <top (required)>'
起初,我认为 RSpec 在渲染视图文件时以某种方式丢失了 content_for 块。但是,我无法在 RSpec 的 github 存储库中找到任何与此相关的问题,所以我不确定这应该归咎于谁。
我在http://www.dixis.com/?p=571 找到了一个(最近的)解决方案。但是,当我尝试建议的代码时
view.instance_variable_get(:@_content_for)
它返回 nil。
- 有没有办法在视图规范中测试 content_for?
- 有没有更好的方法来构建我的布局文件,这样我实际上能够测试它们并仍然获得相同的最终结果?
【问题讨论】:
标签: ruby-on-rails-3 haml rspec2