我有一个运行rspec-rails 2.14.0的rails 3.2.13应用程序,我试图确认视图在我的测试中呈现特定的部分。它实际上确实工作,但我需要添加此测试。这是我到目前为止:Rspec - 测试轨道视图呈现特定的部分
require 'spec_helper'
describe 'users/items/index.html.haml' do
let(:current_user) { mock_model(User) }
context 'when there are no items for this user' do
items = nil
it 'should render empty inventory partial' do
response.should render_template(:partial => "_empty_inventory")
end
end
end
这运行没有错误,但没有通过。失败是:
Failure/Error: response.should render_template(:partial => "_empty_inventory")
expecting partial <"_empty_inventory"> but action rendered <[]>
感谢您的任何想法。
编辑
这对我的作品,但彼得的解决方案是更好的... ...
context 'when there are no items for this user' do
before do
view.stub(current_user: current_user)
items = nil
render
end
it 'should render empty inventory partial' do
view.should render_template(:partial => "_empty_inventory")
end
end
出于某种原因,这是反直觉我有打电话给render
上来看,但你去...
这是什么类型的?它看起来像是一个视图规范,但没有渲染调用。它使用一个'响应'对象,这是一个控制器规范的指示。 –
这是一个视图规范,你是对的。我试图从我找到的不同代码拼凑代码。我在文档中没有看到任何明显的内容:https://www.relishapp.com/rspec/rspec-rails/v/2-8/docs/view-specs/view-spec – panzhuli