这个问题已经解决了.判断底线.

现在,我正在Ruby on Rails上建立我的电影 comments 网站,但我坚持发表 comments .计数错误.已经判断并try 了许多选项,但任何解决方案都不起作用.事实上,我的网站运行得很好,但这个测试仍然显示了错误,我不明白问题是什么以及为什么会发生.你能帮我纠正这个错误吗?非常感谢.

错误代码

 test_comment_interface#CommentsInterfaceTest (3.43s)
        "Comment.count" didn't change by 1.
        Expected: 39
          Actual: 38
        test/integration/comments\u interface\u测试.rb:19:in `block in <class:CommentsInterfaceTest>'
  1. comments\u interface\u测试
require 'test_helper'

class CommentsInterfaceTest < ActionDispatch::IntegrationTest
  
  def setup
    @user = users(:michael)
  end
  
  test "comment interface" do
    log_in_as(@user)
    get root_path
    assert_select 'div.pagination'
    
    assert_no_difference 'Comment.count' do
      post comments_path, params: { comment: { content: "" } }
    end
    assert_select 'div#error_explanation'
    content = "This comment really ties the room together"
    assert_difference 'Comment.count', 1 do  #<-----------------------error point
      post comments_path, params: { comment: { content: content } }
    end
    assert_redirected_to root_url
    follow_redirect!
    assert_match content, response.body
    assert_select 'a', text: 'delete'
    first_comment = @user.comments.paginate(page: 1).first
    assert_difference 'Comment.count', -1 do
      delete comment_path(first_comment)
    end
    get user_path(users(:archer))
    assert_select 'a', text: 'delete', count: 0
  end
end
  1. 议论rb型
class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :movie, optional: true, primary_key: "id"
  has_many :favorites, dependent: :destroy
  
  
  default_scope -> { order(created_at: :desc) }
  validates :user_id,   presence: true
  validates :movie_id,  presence: true
  validates :content,   presence: true, length: { maximum: 250}
  
end
  1. comments\u控制器
class CommentsController < ApplicationController
  include HTTParty
  before_action :logged_in_user, only: [:create, :destroy]
  before_action :correct_user,   only: :destroy
  
  def create
    @comment = current_user.comments.build(comment_params)
    comment_count = Comment.where(movie_id: params[:id]).where(user_id: current_user.id).count
 
    if @comment.valid?
      if comment_count < 1
        @comment.save
        flash[:success] = "Comment created!"
        redirect_to root_url
      else
        flash[:success] = "Can't post a comment twice on the same movie"
        redirect_to request.referrer || root_url
      end
      
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end
  
  def destroy
    @comment.destroy
    flash[:success] = "Comment deleted"
    redirect_to request.referrer || root_url
  end
  
  def edit
    @movie_info = Movie.details(params[:movie_id])
    @comment = Comment.find(params[:id])
    
    if Movie.exists?(@movie_info["id"])
      @movie_db = Movie.find(@movie_info["id"])
      @comments = @movie_db.comments.paginate(page: params[:page])
    end
  end
  
  def update
    @comment = Comment.find(params[:id])
    if @comment.update(comment_params)
      redirect_to root_url
    else
      render :edit
    end
  end
  
  private
  
    def comment_params
      params.require(:comment).permit(:content)
    end
    
    def correct_user
      @comment = current_user.comments.find_by(id: params[:id])
      redirect_to root_url if @comment.nil?
    end
end
  1. comments\u控制器_test
require 'test_helper'

class CommentsControllerTest < ActionDispatch::IntegrationTest
  
  def setup
    @user = users(:michael)
    @comment = comments(:orange)
  end
  
  test "should redirect create when not logged in" do
    assert_no_difference 'Comment.count' do
      post comments_path, params: { comment: { content: "Lorem ipsum" } }
    end
    assert_redirected_to login_url
  end
  
  test "should redirect destroy when not logged in" do
    assert_no_difference 'Comment.count' do
      delete comment_path(@comment)
    end
    assert_redirected_to login_url
  end
  
  test "should redirect destroy for wrong comment" do
    log_in_as(users(:michael))
    comment = comments(:ants)
    assert_no_difference 'Comment.count' do
      delete comment_path(comment)
    end
    assert_redirected_to root_url
  end
end
  1. comments .yml公司
orange:
  content: "I just ate an orange!"
  created_at: <%= 10.minutes.ago %>
  user: michael
  movie: harry
  
tau_manifesto:
  content: "Check out the @tauday site by @mhartl: http://tauday.com"
  created_at: <%= 3.years.ago %>
  user: michael
  movie: harry
  
cat_video:
  content: "Sad cats are sad: http://youtu.be/PKffm2uI4dk"
  created_at: <%= 2.hours.ago %>
  user: michael
  movie: harry
  
most_recent:
  content: "Writing a short test"
  created_at: <%= Time.zone.now.to_s(:db) %>
  user: michael
  movie: harry
  
<% 30.times do |n| %>
comment_<%= n %>:
  content: <%= Faker::Lorem.sentence(5) %>
  created_at: <%= 42.days.ago %>
  user: michael
  movie: harry
<% end %>

ants:
  content: "Oh, is that what you want? Because that's how you get ants!"
  created_at: <%= 2.years.ago %>
  user: archer
  movie: harry

zone:
  content: "Danger zone!"
  created_at: <%= 3.days.ago %>
  user: archer
  movie: harry

tone:
  content: "I'm sorry. Your words made sense, but your sarcastic tone did not."
  created_at: <%= 10.minutes.ago %>
  user: lana
  movie: harry

van:
  content: "Dude, this van's, like, rolling probable cause."
  created_at: <%= 4.hours.ago %>
  user: lana
  movie: harry

根据下面的 comments ,我修复了一些类似这样的代码.最后它成功了,问题解决了.

  1. comments\u interface\u测试
  def setup
    @user  = users(:michael)
    @movie = movies(:harry)  #add
  end

  assert_difference 'Comment.count', 1 do
      post comments_path, params: { comment: { content: content, movie_id: @movie.id } } #fixed
  end
  1. comments\u控制器
  private
  
    def comment_params
      params.require(:comment).permit(:content, :movie_id) #fixed
    end
    

推荐答案

通过查看您的Comment模型,它看起来像是一个验证错误.您在模型上进行了以下验证,但在测试中,您没有指定movie_id.

comment.rb

  validates :user_id,   presence: true
  validates :movie_id,  presence: true

相反,它应该类似于下面的params: { id: movie_id, comment: { content: content } }.

Ruby-on-rails相关问答推荐

本地主机的上衣配置

删除链接不起作用,重定向 echo 示 Ruby

获取所有属于模型的嵌套对象

我需要在我的 Rails 7 应用程序中保留 app/assets/config/manifest.js 吗?

您将如何在 Ruby on Rails 应用程序中使用 rSpec 测试观察者?

如何在 Ruby 字符串中返回最后一个斜杠(/)之后的所有内容

Rails 4,使用 ActiveRecord 的原始查询

使用 javascript 提交 Rails 远程表单

渲染:动作和渲染:模板之间的区别

Rails - 使用 %W

如何在 Rails 中验证日期以使其在今天之后?

如果缺少翻译,则回退到默认语言

使用god 监控独角兽 - 以非零代码开始退出 = 1

Ruby on Rails:调试 rake 任务

如何使用 Ruby 在现有 PDF 上编辑或书写?

如何拯救 OmniAuth::Strategies::OAuth2::CallbackError?

设计记住我和会话

rails 4 before_validation on: :create 或 on: :save

用户注册时设计确认令牌无效

将现有的 html.erb 转换为 Haml