rspec控制器规范匹配器

时间:2016-04-28 20:02:01

标签: ruby-on-rails rspec controller

使用哪个匹配器以及如何测试@post_comment@post_comment.user是否已正确分配?

expect(assigns(:post_comment)).to be_a_new(PostComment)无法在这里工作。

更新:

通过以下设置,我也会收到以下错误。我应该更改什么才能测试无效的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 :(得分:1)

  1. 如何测试这两个?

    for (int i = 0; i < lstShipToAddresses.Items.Count; i++)
    {
        var listBoxItem = lstShipToAddresses.ItemContainerGenerator.ContainerFromIndex(i);
        (listBoxItem as ListBoxItem).Background = Brushes.Magenta; 
    }
    

    我相信你不应该测试一个新的记录,而是一个班级的记录,并坚持记录:

    expect(assigns(:post_comment)).to be_a_new(PostComment)
    expect(assigns(:post_comment.user)).to eq(@user)
    
  2. 代码过多。

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

    你应该只保留单一的记录,我相信它除了例外之外就足够了:

    @post_comment.save!
    if @post_comment.save
    

    您可以从@post_comment.save! 块中选择其他部分代码。来自if的例外情况,您应该使用save!陷阱。

相关问题