我怎样才能捕捉到这个错误:PG::NotNullViolation: ERROR: null value in column

问题描述 投票:0回答:1

我正在用 rspec 进行测试

我有这个功能参数:

def update_section_params
        params.require(:section).permit(:type_id, :by_team)
end

我正在使用更新部分

if @section.update(update_treatment_params)

如果 by_team: nil

我会收到这个错误
PG::NotNullViolation: ERROR:  null value in column 

我怎样才能在我的测试中发现这个错误?

 payload = { by_team: nil }
 put ".../.../sections/#{@section1['id']}", params: payload
ruby-on-rails rspec
1个回答
0
投票

在你的

Section
模型中(可能在
app/models/section.rb
。添加一个
belongs_to
和验证:

class Section < ApplicationRecord
  belongs_to :type

  validates :by_team, presence: true
end
© www.soinside.com 2019 - 2024. All rights reserved.