2013-10-05 108 views
1

關聯模型我有兩個型號:Rspec的不能創建mongoid

文章模型

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 

我可以創建「文章CONSOL意見。 first.comments.create「並在瀏覽器中查看,但是當我測試它時,RSpec不能爲文章創建任何評論。對於任何建議,我將不勝感激。

+0

請分享你收到的錯誤 –

+0

嗨,分享,由於某種原因rspec不能創建評論... – FastIndian

+0

這個錯誤只是說你無法在你的頁面上找到評論正文,並不是說你不能沒有成功創建評論。 –

回答

0

看起來您在訪問該頁面後正在創建註釋。 RSpec中的before塊在第一級外部執行。

嘗試將visit article_path(article)步驟移入內部before塊。

+0

YEAH !!!謝謝!:)))很簡單...我找不到兩天的答案... – FastIndian