标签: shoulda-matchers

rspec shoulda matchers 预计属于多态

我正在尝试测试我的多态关联,但我似乎无法让它通过

模型:

class Foo < ApplicationRecord
  belongs_to :bar, polymorphic: true, optional: true
end
Run Code Online (Sandbox Code Playgroud)

现在我的测试看起来像

RSpec.describe Foo, type: :model do
  subject { build(:foo) }
  it { is_expected.to belong_to(:bar) }
end
Run Code Online (Sandbox Code Playgroud)

我收到的错误

Foo
  is expected to belong to bar required: true (FAILED - 1)

Failures:

>   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 …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails shoulda-matchers

3
推荐指数
1
解决办法
2055
查看次数

标签 统计

rspec ×1

ruby-on-rails ×1

shoulda-matchers ×1