rspec shoulda匹配器预计属于多态
rspec shoulda matchers is expected to belong to polymorphic
我正在尝试测试我的多态关联,但似乎无法通过
型号:
class Foo < ApplicationRecord
belongs_to :bar, polymorphic: true, optional: true
end
现在我的测试看起来像
RSpec.describe Foo, type: :model do
subject { build(:foo) }
it { is_expected.to belong_to(:bar) }
end
我遇到的错误
富
预计属于 bar required: true (FAILED - 1)
失败:
- Foo is expected to belong to bar required: true
Failure/Error: it { is_expected.to belong_to(:bar) }
Expected Foo to have a belongs_to association called bar (and for the record to fail validation if :bar is unset; i.e., either the
association should have been defined with
required: true
, or there
should be a presence validation on :bar)
/# ./spec/models/foo_spec.rb:4:in `block (2 levels) in <top (required)>'
现在这个关联可以是零值
问题似乎是:
and for the record to fail validation if :bar is unset;
因为,你有optional: true
- 这部分不满意。
看起来 shoulda 假设应该需要一个关系,除非你另有说明。
尝试像这样修改这个匹配器
it { is_expected.to belong_to(:bar).optional }
我正在尝试测试我的多态关联,但似乎无法通过
型号:
class Foo < ApplicationRecord
belongs_to :bar, polymorphic: true, optional: true
end
现在我的测试看起来像
RSpec.describe Foo, type: :model do
subject { build(:foo) }
it { is_expected.to belong_to(:bar) }
end
我遇到的错误
富 预计属于 bar required: true (FAILED - 1)
失败:
- Foo is expected to belong to bar required: true Failure/Error: it { is_expected.to belong_to(:bar) } Expected Foo to have a belongs_to association called bar (and for the record to fail validation if :bar is unset; i.e., either the association should have been defined with
required: true
, or there should be a presence validation on :bar) /# ./spec/models/foo_spec.rb:4:in `block (2 levels) in <top (required)>'
现在这个关联可以是零值
问题似乎是:
and for the record to fail validation if :bar is unset;
因为,你有optional: true
- 这部分不满意。
看起来 shoulda 假设应该需要一个关系,除非你另有说明。
尝试像这样修改这个匹配器
it { is_expected.to belong_to(:bar).optional }