2016-04-28 64 views
0

使用哪個匹配器以及如何測試@post_comment@post_comment.user是否已正確分配?rspec控制器spec匹配器

expect(assigns(:post_comment)).to be_a_new(PostComment)不在這裏工作。

UPDATE:

用下面的設置我也碰到下面的錯誤。我應該改變什麼才能測試無效的attrs?

Posts::PostCommentsController when user is logged in POST create with invalid attributes doesn't save the new product in the db 
Failure/Error: @post_comment.save! 

ActiveRecord::RecordInvalid: 
    Validation failed: Body can't be blank 

如果我刪除@post_comment.save!然後我得到

Posts::PostCommentsController when user is logged in POST create with invalid attributes doesn't save the new product in the db 
Failure/Error: <span class="post-comment-updated"><%= local_time_ago(post_comment.updated_at) %></span> 

ActionView::Template::Error: 
    undefined method `to_time' for nil:NilClass 

post_comments_controller

def create 
    @post = Post.find(params[:post_id]) 
    @post_comment = @post.post_comments.build(post_comment_params) 
    authorize @post_comment 
    @post_comment.user = current_user 
    @post_comment.save! 
    if @post_comment.save 
     @post.send_post_comment_creation_notification(@post_comment) 
     @post_comment_reply = PostCommentReply.new 
     respond_to do |format| 
     format.html { redirect_to posts_path, notice: "Comment saved!" } 
     format.js 
     end 
    end 
    end 

post_comments_controller_spec.rb

describe "POST create" do 
    let!(:profile) { create(:profile, user: @user) } 
    let!(:post_instance) { create(:post, user: @user) } 

    context "with valid attributes" do 
    subject(:create_action) { xhr :post, :create, post_id: post_instance.id, post_comment: attributes_for(:post_comment, post_id: post_instance.id, user: @user) } 

    it "saves the new task in the db" do 
     expect{ create_action }.to change{ PostComment.count }.by(1) 
    end 

    it "assigns instance variables" do 
     create_action 
     expect(assigns(:post)).to eq(post_instance) 
     #########How to test these two? 
     #expect(assigns(:post_comment)).to be_a_new(PostComment) 
     #expect(assigns(:post_comment.user)).to eq(@user) 
     expect(assigns(:post_comment_reply)).to be_a_new(PostCommentReply) 
    end 

    it "assigns all the instance variables" 

    it "responds with success" do 
     create_action 
     expect(response).to have_http_status(200) 
    end 
    end 

    context "with invalid attributes" do 
    subject(:create_action) { xhr :post, :create, post_id: post_instance.id, post_comment: attributes_for(:post_comment, post_id: post_instance.id, user: @user, body: "") } 

    it "doesn't save the new product in the db" do 
     expect{ create_action }.to_not change{ PostComment.count } 
    end 
    end 
end 
+1

「是一個新的」 是一個新的未保存記錄 –

+0

'@post_comment = @ post.post_comments.build(post_comment_params)等待'不會被保存。 –

+0

在此期間我發現了另一個問題。我也插入了那個。 –

回答

1
  1. 如何測試這兩個?

    expect(assigns(:post_comment)).to be_a_new(PostComment) 
    expect(assigns(:post_comment.user)).to eq(@user) 
    

    我相信你shoudl測試並不是一個新的記錄,但一類的記錄,並堅持記錄:

    expect(assigns(:post_comment)).to be_a(PostComment) 
    expect(assigns(:post_comment)).to be_presisted 
    expect(assigns(:post_comment.user)).to eq(@user) 
    
  2. 過多的代碼。

    @post_comment.save! 
    if @post_comment.save 
    

    您應保留僅是單一的紀錄,我相信這是足以與例外節省:

    @post_comment.save! 
    

    那麼另一部分代碼,你可以挑選出if塊。來自save!的例外情況應由rescue_from來捕獲。

+0

我試過'@ post_comment.save!'和'if @ post_comment.save'兩種方式,但這兩種方式都會發送某種錯誤。如果我保留'@ post_comment.save!',那麼我得到我列出的第一個錯誤,如果我保留'if'版本,那麼我得到另一個錯誤消息。 –

+0

@SzilardMagyar這很好,'save!'方法告訴你,你沒有填寫body屬性,看你的模型驗證 –

+0

我沒有得到的是爲什麼拋出測試這個錯誤。我也在其他控制器中以類似的方式測試了「具有無效屬性」,並且測試剛剛通過,因爲它也應該在這裏。 –

相關問題