【发布时间】:2018-01-21 16:44:08
【问题描述】:
我正在使用 RSpec 和 Shoulda-Matchers 来测试我的模型。
我知道如何测试模型是否是这样的
class Headquarter < ApplicationRecord
has_many :branches
end
class Branch < ApplicationRecord
belongs_to :headquarter
end
我可以像这样写一个 shoulda-matcher:
RSpec.describe Headquarter, type: :model do
it { should have_many(branches)}
end
RSpec.describe Branch, type: :model do
it { should belong_to(:headquarter)}
end
我的问题
但是当我使用模块化模型时会出现问题,例如Company::Headquarter和Company::Branch
我尝试使用此代码:
RSpec.describe Company::Headquarter, type: :model do
it { should have_many(:company_branches)}
end
但它给了我一个错误,似乎它没有将模型识别为模块化。
Failure/Error: it { should have_many(:company_branches)}
Expected Company::Headquarter to have a has_many association called company_branches (Company::Branch does not have a headquarter_id foreign key.)
当我期待 company_headquarter_id时,请注意错误中的headquarter_id。这就是它无法识别 ID 的原因。
【问题讨论】:
标签: ruby-on-rails rspec