Rspec & ShouldaMatchers 与 class_name
Rspec & ShouldaMatchers with class_name
我收到以下错误:
1) Admin should belong to hospital
Failure/Error: it {should belong_to(:hospital).with_foreign_key('medical_facility_id') }
Expected Admin to have a belongs_to association called hospital ()
与:
#admin_spec.rb
it {should belong_to(:hospital).with_foreign_key('medical_facility_id') }
型号是:
class Admin < ActiveRecord::Base
belongs_to :hospital
end
class Hospital < MedicalFacility
end
在schema.rb
中:
create_table "admins", force: :cascade do |t|
t.integer "user_id", null: false
t.string "role", null: false
t.integer "medical_facility_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
如何正确编写该模型的测试?也许我应该补充
`class_name:"MedicalFacility" 到 Admin 模型?
告诉 Rails 你的外键:
class Admin < ActiveRecord::Base
belongs_to :hospital, foreign_key 'medical_facility_id'
end
我收到以下错误:
1) Admin should belong to hospital
Failure/Error: it {should belong_to(:hospital).with_foreign_key('medical_facility_id') }
Expected Admin to have a belongs_to association called hospital ()
与:
#admin_spec.rb
it {should belong_to(:hospital).with_foreign_key('medical_facility_id') }
型号是:
class Admin < ActiveRecord::Base
belongs_to :hospital
end
class Hospital < MedicalFacility
end
在schema.rb
中:
create_table "admins", force: :cascade do |t|
t.integer "user_id", null: false
t.string "role", null: false
t.integer "medical_facility_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
如何正确编写该模型的测试?也许我应该补充 `class_name:"MedicalFacility" 到 Admin 模型?
告诉 Rails 你的外键:
class Admin < ActiveRecord::Base
belongs_to :hospital, foreign_key 'medical_facility_id'
end