私は2つのモデルを持っています:
記事モデル
class Article
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :body, type: String
default_scope queryable.order_by(:created_at.desc)
scope :archive, -> { where(:created_at.lte => Time.now - 1.month)}
embeds_many :comments, as: :commentable
validates_presence_of :title, :body
end
とコメントモデル
class Comment
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :commentable, polymorphic: true
embeds_many :answers, as: :commentable, class_name: "Comment"
default_scope queryable.order_by(:created_at.asc)
field :body, type: String
validates_presence_of :body
end
私のテスト:
describe "Article#show" do
let!(:article) {FactoryGirl.create(:article)}
before{visit article_path(article)}
subject{ page }
describe "can be commented by other user" do
before do
@comment1 = article.comments.create(body: Faker::Lorem.paragraph)
@comment2 = article.comments.create(body: Faker::Lorem.paragraph)
@comment3 = article.comments.create(body: Faker::Lorem.paragraph)
end
it {should have_selector("div.comment_text", text: @comment1.body)}
it {should have_selector("div.comment_text", text: @comment2.body)}
it do
#save_and_open_page
should have_selector("div.comment_text", text: @comment3.body)}
end
end
エラー:
Failure/Error: it {should have_selector("div .comment_text", text: @comment1.body)}
Capybara::ExpectationNotMet:
expected to find css "div .comment_text" with text "Aliquam omnis et ullam quae
maiores voluptates. Rerum dicta dolores alias voluptates rerum voluptas autem.
Nemo non quia possimus. Nam commodi odio quia et. Aut non odit ratione quas sit accusantium ut." but there were no matches
ビューでは、私は部分的です:
=render "shared/comments", model: @article
部分的(コンソールからコメントが作成されたときに正しい結果を表示):
.comments
-model.comments.each do |comment|
.comment
= image_tag("delete/brian.jpg", class: "avatar")
.comment-author
=Faker::Name.name
%time.comment-date= l(comment.created_at)
.comment-text
= comment.body
-if comment.answers.any?
-comment.answers.each do |answer|
.comments-answers
=image_tag('delete/brian.jpg',class: "avatar")
.comment-author
=Faker::Name.name
%time.comment-date= l(comment.created_at)
.comment-text= answer.body
「Article.first.comments.create」でコンソールからコメントを作成してブラウザで見ることができますが、テストしているとき、RSpec は記事のコメントを作成できません。アドバイスをいただければ幸いです。