关注instructions我安装了 mongoid-rspec 并在spec_helper.rb中对其进行了配置
RSpec.configure do |config|
config.include Mongoid::Matchers, type: :model
end
但是遇到了最简单的测试问题
describe City do
it { should have_many(:locations) }
end
City should have many :locations
Failure/Error: it { should have_many(:locations) }
NoMethodError:
undefined method `has_many?' for
似乎我做错了,但无法弄清楚究竟是什么错误。
模型非常简单
class City
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paranoia
include Mongoid::Versioning
max_versions 10
has_many :locations
end
class Location
include Mongoid::Document
belongs_to :city
field :name, type: String
end
最后一件事,我不使用Rails。
答案 0 :(得分:1)
在我的spec_helper文件中,我删除了选项类型:: model,现在可以正常工作
RSpec.configure do |config|
config.include Mongoid::Matchers
end
答案 1 :(得分:1)
我遇到了类似的问题,但在使用导轨时,我认为导轨和无导轨的解决方案非常相似:
gem 'mongoid-rspec', '~> 2.2.0'
没有导轨
添加到spec_helper.rb文件:
require 'mongoid-rspec'
RSpec.configure do |config|
config.include Mongoid::Matchers
end
使用rails
添加到您的rails_helper.rb
require 'mongoid-rspec'
RSpec.configure do |config|
config.include Mongoid::Matchers, type: :model
end