测试活动记录关联时出错



我有一个具有以下模型的Rails 5.2应用程序:

class Foo < ApplicationRecord
has_many :bars
end
class Bar < ApplicationRecord
belongs_to :foo
end

我正在使用 rspec-rails (3.7.2(,应该匹配器 (3.1.2(。 我有一个规范来测试关联:

describe Foo do
it { is_expected.to have_many :bars }
it { should have_many :bars }
end

无论我使用哪种语法,我都会得到相同的错误:

Foo should have many :bars
Failure/Error: it { is_expected.to have_many :bars }
expected #<Foo:0x007f9f813ce650> to respond to `has_many?` # ./spec/models/foo_spec.rb:4
Foo should have many :bars
Failure/Error: it { should have_many :bars }
expected #<Foo:0x007f9f84ad3970> to respond to `has_many?` # ./spec/models/foo_spec.rb:5

我按照这里的说明进行操作,但仍然收到错误。我已经将我的示例项目推送到 Github:https://github.com/fredwillmore/shoulda_matchers_test

我不确定,但我认为您需要定义 rspec 的类型,例如:

RSpec.describe Foo, type: :model do
it { is_expected.to have_many :bars }
end

相关内容

最新更新