我创建了一个以mongoid作为odm的Rails 4应用程序。我可以使用mongoid-rspec gem成功测试所有模型,但无法嵌入文档。我找不到办法。
这是基本模型:
class Project::Type
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paranoia
store_in session: 'project', collection: 'type'
embeds_many :rules, :class_name => "Project::TypeRule"
field :name, type: String
field :timing, type: String
index({ name: 1 }, { unique: false, name: 'project_type_name', background: true })
end
这是嵌入式文档:
class Project::TypeRule
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paranoia
embedded_in :type ,:inverse_of => :rules
TYPES = ["internal_link", "external_link","h1"]
field :name, type: String
field :rule_type, type: String
field :value, type: String
field :count, type: Integer
end
这是测试脚本:
require 'rails_helper'
describe Project::Type, :type => :model do
let(:type) { create(:project_type) }
it { should be_stored_in :type }
# Associations
it { should embed_many(:rules) }
#Fields
it { should have_fields(:name).of_type(String) }
it { should have_fields(:timing).of_type(String) }
it { should be_timestamped_document }
it { should be_paranoid_document }
#Indexes
it { should have_index_for(name: 1).with_options(unique: false, name: 'project_type_name', background: true) }
end
我已经在Google上搜索了有关该内容的信息,但不幸的是没有任何线索。测试嵌入式文档的正确方法是什么?
您正在测试Type嵌入了许多规则。现在,您应该测试另一面:
describe Project::TypeRule, :type => :model do
# Associations
it { should be_embedded_in(:type).as_inverse_of(:rules) }
# Fields
it { should have_fields(:name).of_type(String) }
...
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句