pos*_*eid 4 activerecord rspec
我正在玩一个关于在RSpec中通过关联测试has_many的示例.我得到了一个
1) Foo specifies items
Failure/Error: subject.should have_many(:items)
NoMethodError:
undefined method `has_many?' for #
# ./spec/models/foo_spec.rb:10
我的问题:为什么has_many会被定义?
规格是:
describe Foo do
it "specifies items" do
subject.should have_many(:items)
end
end
Run Code Online (Sandbox Code Playgroud)
我的模特是:
foo.rb:
class Foo < ActiveRecord::Base
has_many :bars
has_many :items, :through => :bars
end
Run Code Online (Sandbox Code Playgroud)
bar.rb:
class Bar < ActiveRecord::Base
belongs_to :foo
belongs_to :item
end
Run Code Online (Sandbox Code Playgroud)
和item.rb:
class Item < ActiveRecord::Base
has_many :foos, :through => :bars
has_many :bars
end
Run Code Online (Sandbox Code Playgroud)
好吧,has_many?模型对象上没有方法.并且rspec-rails默认情况下不提供此类匹配器.然而,shoulda-matchers宝石确实:
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
Run Code Online (Sandbox Code Playgroud)
(例如来自shoulda-matcher的文档)
只需添加gem 'shoulda-matchers'到您的Gemfile,您就可以使用该语法.
| 归档时间: |
|
| 查看次数: |
3468 次 |
| 最近记录: |