Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 用Factory_Girl建立嵌入式Mongoid模型_Ruby On Rails 3_Rspec_Mongoid_Factory Bot - Fatal编程技术网

Ruby on rails 3 用Factory_Girl建立嵌入式Mongoid模型

Ruby on rails 3 用Factory_Girl建立嵌入式Mongoid模型,ruby-on-rails-3,rspec,mongoid,factory-bot,Ruby On Rails 3,Rspec,Mongoid,Factory Bot,所以我在玩Mongoid、Rspec和Factory_Girl,我在嵌入文档时遇到了一些问题 我有以下型号: class Profile include Mongoid::Document #Fields and stuff embeds_one :address validates :address, presence: true end class Address include Mongoid::Document #Fie

所以我在玩Mongoid、Rspec和Factory_Girl,我在嵌入文档时遇到了一些问题

我有以下型号:

class Profile    
   include Mongoid::Document

   #Fields and stuff
      embeds_one :address

   validates :address, presence: true 
end

class Address    
   include Mongoid::Document

   #Fields and stuff
      embedded_in :profile 
end
所以当我定义这样一个工厂时:

FactoryGirl.define do
  factory :profile do
    #fields

    address
  end
end
Failure/Error: subject { build :profile }
     Mongoid::Errors::NoParent:

       Problem:
         Cannot persist embedded document Address without a parent document.
       Summary:
         If the document is embedded, in order to be persisted it must always have a reference to it's parent document. This is most likely cause by either calling Address.create or Address.create! without setting the parent document as an attribute.
       Resolution:
         Ensure that you've set the parent relation if instantiating the embedded document direcly, or always create new embedded documents via the parent relation.
FactoryGirl.define do
  factory :profile do
    #fields

    after(:build) do |p| 
      p.create_address(FactoryGirl.attributes_for(:address))
    end
  end
end
我犯了这样一个错误:

FactoryGirl.define do
  factory :profile do
    #fields

    address
  end
end
Failure/Error: subject { build :profile }
     Mongoid::Errors::NoParent:

       Problem:
         Cannot persist embedded document Address without a parent document.
       Summary:
         If the document is embedded, in order to be persisted it must always have a reference to it's parent document. This is most likely cause by either calling Address.create or Address.create! without setting the parent document as an attribute.
       Resolution:
         Ensure that you've set the parent relation if instantiating the embedded document direcly, or always create new embedded documents via the parent relation.
FactoryGirl.define do
  factory :profile do
    #fields

    after(:build) do |p| 
      p.create_address(FactoryGirl.attributes_for(:address))
    end
  end
end
我通过将工厂改成这样的方式来实现这一目标:

FactoryGirl.define do
  factory :profile do
    #fields

    address
  end
end
Failure/Error: subject { build :profile }
     Mongoid::Errors::NoParent:

       Problem:
         Cannot persist embedded document Address without a parent document.
       Summary:
         If the document is embedded, in order to be persisted it must always have a reference to it's parent document. This is most likely cause by either calling Address.create or Address.create! without setting the parent document as an attribute.
       Resolution:
         Ensure that you've set the parent relation if instantiating the embedded document direcly, or always create new embedded documents via the parent relation.
FactoryGirl.define do
  factory :profile do
    #fields

    after(:build) do |p| 
      p.create_address(FactoryGirl.attributes_for(:address))
    end
  end
end
这是可行的,但我希望有一个更本土的工厂女孩的方式来做这件事。看来应该有


提前谢谢

尝试使用
build\u address
而不是
create\u address
。在我看来,您的工厂被破坏了,因为您试图在配置文件记录被持久化(创建)之前创建一个地址记录<代码>构建*应将所有必要的属性分配给父模型,之后应将其与其嵌入的关系一起保存。

您也可以这样做,如中所述:


很公平,但是FactoryGirl中没有回调就无法处理这个问题吗?我不这么认为,这是mongodb/mongoid特定的东西。这在FactoryGirl 4.4.0中有效,但在4.5.0中似乎不再有效。请参阅我的(迄今尚未回答的)问题: