我有一个后和标记模型之间的多对多关联。 然后,我有我的信息视图以下内容:如何为@ post.tags.map之类的内容编写规范?
帖子/ show.html.erb:
<p><%= raw @post.tags.map { |t| link_to t.name, tag_path(t.name) }.join(', ') %></p>
这是我写的规格:
post_pages_spec.rb:
describe "show page" do
let!(:post) { FactoryGirl.create(:post, user: user,
title: "Lorem",
content: "Lorem ipsum",
category_id: category.id,
tag_list: "#{tag.id}") }
before do
sign_in user
visit post_path(post)
end
it { should have_selector('h1', text: post.title) }
it { should have_selector('title', text: post.title) }
it { should have_link(post.user.name, href: user_path(user)) }
it { should have_selector('p', text: post.content) }
it { should have_selector('p', text: post.category.name) }
it { find("p").should have_content(post.tags.map { |t| t.name }.join(", ")) }
.
.
.
我得到这个错误:
Failures:
1) Post pages show page Failure/Error: it { find("p").should have_content(post.tags.map { |t| t.name }.join(", ")) } expected there to be content "1, tag28" in "Lorem ipsum" # ./spec/requests/post_pages_spec.rb:28:in `block (3 levels) in '
该代码在真实网站中工作,但正如您所看到的,该规范失败。什么是写这个规范的正确方法?
编辑:
桩模型(以防万一)的一个例子:从活动现场
class Post < ActiveRecord::Base
include ActionView::Helpers
attr_accessible :title, :content, :category_id, :tag_list
has_many :taggings, :dependent => :destroy
has_many :tags, :through => :taggings
.
.
.
输出:
<p><a href="/tags/inkscape">inkscape</a>, <a href="/tags/gimp">gimp</a></p>
包含实际的'page.body',失败仅仅是说找到的元素有内容“Lorem ipsum”,所以它可能是由于许多可能的原因找到了错误的元素 – prusswan
@prusswan对不起,什么你的意思是包含page.body? – alexchenco
'page.body'是测试收到的实际html内容。 'page.body.should have_content ...'常见,你可以打印它来检查是否有缺失的元素 – prusswan