我的模型是Attendance
,因为它是 Schema :
create_table "attendances", force: :cascade do |t|
t.date "date"
t.time "time_in"
t.time "time_out"
t.float "late_minutes"
t.float "over_time"
t.float "time_spent"
t.boolean "late"
t.boolean "absent"
t.boolean "off"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "company_id"
t.bigint "public_holiday_id"
t.bigint "leave_type_id"
t.integer "employee_id"
t.integer "employee_code"
t.index ["company_id"], name: "index_attendances_on_company_id"
t.index ["employee_code"], name: "index_attendances_on_employee_code"
t.index ["employee_id"], name: "index_attendances_on_employee_id"
t.index ["leave_type_id"], name: "index_attendances_on_leave_type_id"
t.index ["public_holiday_id"], name: "index_attendances_on_public_holiday_id"
end
出勤模式:
class Attendance < ApplicationRecord
include AttendanceConcerns
validates :date, presence: true
validates :company_id, presence: true
validates :employee_id, presence: true
validates :employee_code, presence: true
validate :avoid_duplication, on: :create
belongs_to :employee
end
我的 RSpec验证,用于验证状态:
describe 'validates presence' do
before do
@company = Company.new(id: 100, name: "test.com", address: "GulbergIII", contact_number: "03214083601", mobile_number: "23214083601", website: "www,clustox.com", timezone: "+5", created_at: "2018-06-06 11:39:41", updated_at: "2018-06-06 11:39:41")
@user = User.create(id: 250, email: "ayesha.saeed@abc.com", created_at: "2018-07-11 09:37:05", updated_at: "2019-07-12 05:26:01", company_id: @company.id, first_name: nil, last_name: nil, middle_name: nil, img: "2.png")
@shift = Shift.create(id: 110, start_time: "2000-01-01 10:00:00", end_time: "2000-01-01 19:00:00", active: true, created_at: "2018-09-10 11:44:27", updated_at: "2018-12-05 07:59:23", company_id: @company.id, title: "10am To 7pm", monday: true, tuesday: true, wednesday: true, thursday: true, friday: true, saturday: false, sunday: false)
@role = Role.create(id: 200, name: "employee", created_at: "2018-11-15 14:17:39", updated_at: "2018-11-15 14:17:39")
@employee = Employee.create(id: 2000, first_name: "Ayesha", middle_name: "", last_name: "Saeed",father_name: "", gender: "female", contact_number: "", date_of_joining: "2018-01-01", date_of_birth: "1994-08-26", nic_number: "xxxx-xxxxxxx-x", marital_status: "Single", home_town: "", designation: "Software Engineer", job_title: "", degree: "", cgpa: nil, university: "", role: nil, salary: nil, hourly_rate: nil, probation_in_month: nil, probation_end_date: nil, status: "hired", created_at: "2018-07-12 07:10:40", updated_at: "2018-10-03 07:31:14", company_id: @company.id, user_id: @user.id, shift_id: @shift.id, employee_code: 28, active: true, approval_authority: false, remote_checkin: "not_allowed", attendance_code: nil, attendence_auto_approval: false, role_id: @role.id)
end
let(:attendance) { Attendance.new(id: 1000, date: "2018-09-03", time_in: "2000-01-01 10:07:22", time_out: "2000-01-01 19:09:23", late_minutes: 142.0, over_time: 43763.0, time_spent: 32521.0, late: true, absent: nil, off: nil, created_at: "2018-10-03 08:12:14", updated_at: "2018-10-03 08:12:14", company_id: @company.id, public_holiday_id: nil, leave_type_id: nil, employee_id: @employee.id, employee_code: @employee.employee_code) }
it 'should allow' do
expect(attendance).to be_valid
end
end
当我运行rspec验证时,它失败并给出以下错误:
Failures:
1) Attendance validates presence should allow
Failure/Error: employee = Employee.where(company_id: self.company.id, user_id: self.user.id)
NoMethodError:
undefined method `id' for nil:NilClass
当我在rails console
上运行完全相同的命令时,它工作得很好,但验证不起作用。
编辑 完整的终端错误日志:
validates presence
should allow (FAILED - 1)
Failures:
1) Attendance validates presence should allow
Failure/Error: employee = Employee.where(company_id: self.company.id, user_id: self.user.id)
NoMethodError:
undefined method `id' for nil:NilClass
# ./app/models/employee.rb:119:in `validate_company_user_association'
# ./spec/models/attendance_spec.rb:30:in `block (3 levels) in <main>'
Finished in 8.09 seconds (files took 2.54 seconds to load)
15 examples, 1 failure
Failed examples:
rspec ./spec/models/attendance_spec.rb:34 # Attendance validates presence should allow
答案 0 :(得分:1)
看起来公司对象是nil
将@company = Company.new(...)
更改为@company = Company.create(...)