我创建了一个带有mongoid作为odm的rails 4应用程序。我可以成功地使用mongoid-rspec gem测试所有模型,但我无法使用emebedded文档。我找不到办法做到这一点。
这是基本模型:
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上搜索过这个但不幸的是没有任何线索。 测试嵌入式文档的正确方法是什么?
答案 0 :(得分:0)
您正在测试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