【问题标题】:rspec controller specs testing post actionrspec 控制器规格测试后操作
【发布时间】:2016-04-28 21:27:23
【问题描述】:

由于某种原因,post spec 工作正常,但 post_comment_spec 测试失败。 post有很多post_cmets,post_comment属于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

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_cmets_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

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_cmets_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

为帖子创建.js.erb

$("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 %>

为 post_comment 创建.js.erb

<% 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 %>

【问题讨论】:

  • Neven,我真的不明白你的问题。
  • post_comment 未保存,因此 updated_at 为零
  • 那我该如何测试呢?我的意思是代码很好,因为它只有在正确保存的情况下才会呈现。
  • 您在尝试创建空白评论时会出现什么行为? create.js.erb 期望 @post_comment 有效...
  • 我没有看到代码很好,做render js有错误,在测试中看到它,但是如果它通过了有效的属性,为什么它是错误的?

标签: ruby-on-rails rspec controller


【解决方案1】:

正如错误所述,对 local_time_ago 的调用最终会尝试在 nil 上调用 to_time - 因此您的评论在其 updated_at 属性中没有任何内容。

post_cmets_controller.rb中,你为什么注释掉#@post_comment.save!

@post_comment.save 返回的是什么?我怀疑由于某种原因保存失败,但您仍在尝试渲染 create.js.erb,它需要一个有效的 @post_comment 对象。

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

【讨论】:

  • Brian 这里是原始问题:stackoverflow.com/questions/36924449/… 我注释掉了,因为如果我也使用@post_comment.save!,它会在测试中发送验证错误,这非常奇怪。顺便说一句,我不明白为什么我最初放在那里,因为代码现在可以正常工作。只是测试有问题。
  • 验证错误是这里的问题 - 由于 save 失败,您正在尝试使用未持久对象呈现 create.js.erb。验证错误是什么?
  • 更新了问题。 Body can't be blank 是错误,这没关系,因为我在测试中通过了 body: ""
  • 所以您的post_comment 模型中必须有validate :body, presence: true,并且由于您传入“”,因此验证失败。验证失败导致@post_comment.save 返回false,然后@post_comment 不是有效对象,因此尝试从create.js.erb 访问它不起作用。
  • Brian,为什么它使用 post 而不是 post_comment?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多