我在Post和Tag模型之间有一个多对多的关联 。然后,我的帖子视图中有以下内容:
帖子/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(", ")) }
.
.
.
我收到此错误:
失败:
1) 帖子页面显示页面失败/错误:它 { find("p").should have_content(post.tags.map { |t| t.name }.join(", ")) } 期望有内容“ 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>