我在staffs表上定义了一个列'full_name',该列也设置为非null。当我运行以下测试(patient_test.rb)时,我得到下面给出的终端输出。因为我没有对员工表进行测试,所以我不明白为什么会出现这个错误。
当我甚至没有在该特定型号上运行测试时,有人可以建议我为什么一直收到此错误。
员工表的架构
create_table "staffs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "full_name", null: false
t.string "first_name", null: false
t.string "middle_name"
t.string "last_name", null: false
t.boolean "gender", null: false
t.string "telephone", null: false
t.string "email", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
测试文件(patient_test.rb)
require 'test_helper'
class PatientTest < ActiveSupport::TestCase
test "should not save user without patient" do
patient = Patient.new
assert_not user.save
end
end
终端输出。
rake test
(in /home/user/Rubymine/project1)
Run options: --seed 51365
# Running:
E
Error:
PatientTest#test_should_not_save_user_without_patient:
ActiveRecord::StatementInvalid: Mysql2::Error: Field 'full_name' doesn't have a default value: INSERT INTO `staffs` (`created_at`, `updated_at`, `id`) VALUES ('2017-03-28 08:45:58', '2017-03-28 08:45:58', 980190962)
bin/rails test test/models/patient_test.rb:9
Finished in 0.112169s, 8.9151 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
此外,我的staff.rb文件只包含关系和范围(如果有)。
Staff.rb文件
class Staff < ApplicationRecord
has_one :user
has_many :time_slots
validates_presence_of :full_name
end
/fixtures/staffs.yml
的内容# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
答案 0 :(得分:1)
这是因为您已在staffs表上声明了大多数列的null false条件。 Rails在运行每个测试运行之前加载所有灯具,因此当灯具'staffs.yml'被命中时,rails会立即引发错误,因为灯具有两个记录(一个和两个)没有数据库所需的值。
你基本上有3个选择: