rspec控制器规范测试后行动 [英] rspec controller specs testing post action

查看:206
本文介绍了rspec控制器规范测试后行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因, post spec 工作正常,但 post_comment_spec 测试失败。帖子有许多post_comments,post_comment属于帖子。

For some reason the post spec working fine but the post_comment_spec test fails. Post has many post_comments, post_comment belongs to post.

错误:

1) 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

posts_controller

posts_controller

def create
  @post = current_user.posts.new(post_params)
  authorize @post
  if @post.save
    respond_to do |format|
      format.js
    end
  else
    respond_to do |format|
      format.js
    end
  end
end  

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.js
    end
  else
    respond_to do |format|
      format.js
    end
  end
end

posts_controller_spec.rb

posts_controller_spec.rb

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

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

    it "doesn't save the new product in the db" do
      expect{ create_action }.to_not change{ Post.count }
    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 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

create.js.erb for post

create.js.erb for post

$("ul.errors").html("");
<% if @post.errors.any? %>
  $('.alert-danger').show();
  $('ul.errors').show();
  <% @post.errors.full_messages.each do |message| %>
    $("ul.errors").append($("<li />").html("<%= message.html_safe %>"));
  <% end %>
<% else %>
  $('ul.errors').hide();
  $('.alert-danger').hide();
  $('.post-index').prepend('<%= j render @post %>');
  collectionPostEditDropdown();
  $('.post-create-body').val('');
  $('#img-prev').attr('src', "#");
  $('#img-prev').addClass('hidden');
  $('.btn-create-post').prop('disabled',true);
  $('.remove-post-preview').addClass('hidden');
<% end %>

create.js.erb for post_comment

create.js.erb for post_comment

<% unless @post_comment.errors.any? %>
  $("#post-comment-text-area-<%= @post_comment.post_id%>").val('');
  $(".post-comment-insert-<%= @post_comment.post_id %>").prepend('<%= j render @post_comment %>');
  $(".best_in_place").best_in_place();
<% end %>


推荐答案

$ c> local_time_ago 最后尝试在nil上调用 to_time ,因此您的注释在其 updated_at中没有任何内容

As the error states, the call to local_time_ago ends up trying to call to_time on nil - so your comment doesn't have anything in its updated_at attribute.

post_comments_controller.rb 中,为什么您要注释#@ post_comment .save!

In post_comments_controller.rb, why did you comment out #@post_comment.save!?

还有什么是 @ post_comment.save 我怀疑保存失败的某些原因,但你仍然试图渲染 create.js.erb 这是期待一个有效的 @post_comment object。

And what is @post_comment.save returning? I suspect the save is failing for some reason, but you're still trying to render create.js.erb which is expecting a valid @post_comment object.

编辑:你不应该渲染 create.js.erb @ post_comment.save 失败,因为它期望有效的 @post_comment 对象。在这种情况下,你可能想要渲染json:nil,status::ok

You shouldn't be rendering create.js.erb when @post_comment.save fails, since it's expecting a valid @post_comment object. You probably want to render json: nil, status: :ok in that case.

这篇关于rspec控制器规范测试后行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆