[英]how to Deleting and destroying embedded documents in Mongoid with Rails 4
[英]How to test mongoid's embedded documents?
我創建了一個以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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.