rspec controller specs testing post action
<p>For some reason the <code>post spec</code> working fine but the <code>post_comment_spec</code> test fails. Post has many post_comments, post_comment belongs to post.</p>

<p>error:</p>

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

 ActionView::Template::Error:
   undefined method `to_time' for nil:NilClass
</code></pre>

<p>posts_controller  </p>

<pre><code>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  
</code></pre>

<p>post_comments_controller</p>

<pre><code>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
</code></pre>

<p>posts_controller_spec.rb</p>

<pre><code>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
</code></pre>

<p>post_comments_controller_spec.rb</p>

<pre><code>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
</code></pre>

<p>create.js.erb for post</p>

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

<p>create.js.erb for post_comment</p>

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