简体   繁体   English

rspec控制器规格匹配器

[英]rspec controller spec matchers

With which matcher and how can I test if the @post_comment and @post_comment.user is properly assigned? 使用哪个匹配器,如何测试@post_comment@post_comment.user @post_comment是否正确分配?

expect(assigns(:post_comment)).to be_a_new(PostComment) is not working here. expect(assigns(:post_comment)).to be_a_new(PostComment)在这里不起作用。

UPDATE: 更新:

With the following setup I also get the following error. 使用以下设置,我还会收到以下错误。 What should I change to be able to test the invalid 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

IF I delete @post_comment.save! 如果我删除@post_comment.save! then I get 然后我得到

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 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 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. How to test these two? 如何测试这两个?

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

    I believe you shoudl test not a new record, but a record of a class, and persisted record: 我相信您应该测试的不是新记录,而是类的记录和持久记录:

     expect(assigns(:post_comment)).to be_a(PostComment) expect(assigns(:post_comment)).to be_presisted expect(assigns(:post_comment.user)).to eq(@user) 
  2. Excessive code. 代码过多。

     @post_comment.save! if @post_comment.save 

    You shall to keep only the single record of that, I believe it is enough save with exception: 您应仅保留该记录的单个记录,我相信除以下情况外,就足够了:

     @post_comment.save! 

    So other part code you can pick out of if block. 因此,您可以从if块中挑选其他零件代码。 Exception from save! save!例外save! you shall to trap with rescue_from . 您应该使用rescue_from进行陷井。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM