Activerecord 为什么使用未定义的方法;“你有很多”;在Rspec示例中?

Activerecord 为什么使用未定义的方法;“你有很多”;在Rspec示例中?,activerecord,rspec,Activerecord,Rspec,我正在玩一个例子,通过RSpec中的关联来测试a。 我得到一份工作 1) Foo specifies items Failure/Error: subject.should have_many(:items) NoMethodError: undefined method `has_many?' for # # ./spec/models/foo_spec.rb:10 我的模型是: foo.rb: class Foo < Act

我正在玩一个例子,通过RSpec中的关联来测试a。 我得到一份工作

1) Foo specifies items Failure/Error: subject.should have_many(:items) NoMethodError: undefined method `has_many?' for # # ./spec/models/foo_spec.rb:10 我的模型是:

foo.rb:

 class Foo < ActiveRecord::Base
   has_many :bars
   has_many :items, :through => :bars
 end
class Foo:条
终止
bar.rb:

class Bar < ActiveRecord::Base
  belongs_to :foo
  belongs_to :item
end
类栏
和item.rb:

class Item < ActiveRecord::Base
  has_many :foos, :through => :bars
  has_many :bars
end
class项:条
有很多酒吧吗
终止

嗯,在模型对象上没有
有很多方法。默认情况下,
rspec-rails
不提供这种匹配器。但是,
shoulda matchers
gem应该:

describe Post do
  it { should belong_to(:user) }
  it { should have_many(:tags).through(:taggings) }
end

describe User do
  it { should have_many(:posts) }
end
(来自shoulda matchers的示例)

只需将
gem'shoulda matchers'
添加到
gem文件
中,就可以使用该语法

describe Post do
  it { should belong_to(:user) }
  it { should have_many(:tags).through(:taggings) }
end

describe User do
  it { should have_many(:posts) }
end