Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/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
Testing FactoryGirl和获取具有所有关联的属性哈希_Testing_Ruby On Rails 4_Associations_Factory Bot - Fatal编程技术网

Testing FactoryGirl和获取具有所有关联的属性哈希

Testing FactoryGirl和获取具有所有关联的属性哈希,testing,ruby-on-rails-4,associations,factory-bot,Testing,Ruby On Rails 4,Associations,Factory Bot,我有两个相关型号–客户,其中有一个地址。这些是我的工厂: FactoryGirl.define do factory :customer do address end end FactoryGirl.define do factory :address do company_name "MyString" … end end 在我的控制器规范中,我尝试为一个客户获取一个包含地址属性的哈希值……这对我来说根本不起作用 第一次尝试是为(:customer)

我有两个相关型号–
客户
,其中有一个
地址
。这些是我的工厂:

FactoryGirl.define do
  factory :customer do
    address
  end
end

FactoryGirl.define do
  factory :address do
    company_name "MyString"
    …
  end
end
在我的控制器规范中,我尝试为一个客户获取一个包含地址属性的哈希值……这对我来说根本不起作用

第一次尝试是为(:customer)使用
属性,但忽略了任何关联(如文档中所述)。在谷歌搜索之后,我找到了使用
FactoryGirl.build(:customer).attributes.Symbolic_keys
的建议,其中应该包括关联参数。但不是对我来说,我只是得到了
{“id”=>…,“created_at”=>…,“updated_at”=>…}
。但是
customer.address.attributes
输出正确的散列,因此关联似乎是正确的

因此,我有一个具有有效地址的正确客户,并且希望得到一个包含所有属性的哈希,这样我就可以测试e。G在我的控制器规范中创建客户

post :create, {customer: !?!?!?}
为了完整起见,以下是我的模型:

class Customer < ActiveRecord::Base
  has_one :address, foreign_key: :entity_id

  validates_presence_of :address
  accepts_nested_attributes_for :address
end

class Address < ActiveRecord::Base
  belongs_to :customer, foreign_key: :entity_id
end
class客户
FactoryGirl不支持此操作。请在此处查看此问题:

我认为这不是什么大问题。写下这句话:

FactoryGirl.attributes_for(:customer, address: FactoryGirl.attributes_for(:address))
最后是绿色:-)我最终得到了post:create,customer:attributes\u for(:customer,address\u attributes:attributes\u for(:address))