Rspec& ShouldaMatchers with class_name

时间:2016-04-08 10:30:39

标签: ruby-on-rails rspec shoulda

我收到了以下错误:

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:&#34; MedicalFacility&#34;管理模型?

1 个答案:

答案 0 :(得分:1)

告诉Rails您的异国情调外键:

class Admin < ActiveRecord::Base
  belongs_to :hospital, foreign_key 'medical_facility_id'
end