通过FactoryBot创建记录时遇到问题,该问题仅在test
环境下发生。
当我在development控制台上运行命令时:
FactoryBot.create(:accounting_pbs_reservation, factor: 3.0)
(列factor
也在工厂定义,但我想显式传递它)
然后正确执行并创建了它,但是当我在test环境下运行命令时,它会显示错误:
ActiveRecord::NotNullViolation:
Mysql2::Error: Column 'factor' cannot be null:
INSERT INTO `accounting_pbs_reservations` (`start`, `rl_walltime`, `factor`)
VALUES (1503468000, 1430000, 3.0)
即使显示factor列也是如此。
当我想在RSpec控制器中创建模型的实例时,也会发生此问题。
有人遇到同样的问题吗? test
和development
]的数据库模式
factor
的数据库模式设置为
| Field | Type | Null | Key | Default | Extra | factor | double | NO | | 1 |
FactoryBot定义很漂亮:
FactoryBot.define do factory :accounting_pbs_reservation do factor { 1.0 } start { 1_503_468_000 } rl_walltime { 1_430_000 } end end
发生错误的RSpec定义:
require 'rails_helper'
RSpec.describe AccountingPbsReservationsController, type: :controller do
let(:valid_accounting_pbs_reservation) { FactoryBot.create(:accounting_pbs_reservation) }
let(:valid_attributes) { FactoryBot.attributes_for(:accounting_pbs_reservation) }
...
context 'when logged as admin' do
login_admin
it "returns a success response" do
get :show, params: { id: valid_accounting_pbs_reservation.to_param expect(response).to be_successful
end
...
我通过FactoryBot创建记录时遇到问题,该问题仅在测试环境中发生。当我在开发控制台上运行命令时:FactoryBot.create(:...
所以问题出在数据库触发器上。