【发布时间】:2013-12-15 14:33:14
【问题描述】:
我不想用 Rspec 在 Rails 中为视图编写规范。我是这样写的:
require 'spec_helper'
describe "homepage/index.html.erb" do
it 'has list of publications with id #publications' do
render
expect(rendered).to have_selector('ul#publications')
end
it 'has publications on the list' do
assign(:publications, [
stub_model(Publication, content: 'First publication.'),
stub_model(Publication, content: 'Second publication.')
])
render
rendered.should have_content('First publication.')
rendered.should have_content('Second publication.')
end
end
但stub_model 似乎不起作用。
rspec
.....F.
Failures:
1) homepage/index.html.erb has list of publications with id #publications
Failure/Error: render
ActionView::Template::Error:
undefined method `each' for nil:NilClass
# ./app/views/homepage/index.html.erb:2:in `_app_views_homepage_index_html_erb___3273264012123365698_43368240'
# ./spec/views/homepage/index.html.erb_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.17634 seconds
7 examples, 1 failure
我怎样才能正确地存根?
【问题讨论】:
-
我对你的问题投了反对票,因为你没有提供
index.html.erb的相关代码。 -
我认为这很明显......但无论如何我问得太快了。失败的测试是第一个,而不是第二个。所以我将分配转移到
before并且效果很好。
标签: ruby-on-rails testing view rspec specifications