【发布时间】:2013-12-22 13:12:31
【问题描述】:
我有两个相关的模型 - Customer,其中有一个 Address。这些是我的工厂:
FactoryGirl.define do
factory :customer do
address
end
end
FactoryGirl.define do
factory :address do
company_name "MyString"
…
end
end
在我的控制器规范中,我尝试为客户获取包含地址属性的哈希值……这对我不起作用。
第一次尝试是使用attributes_for(:customer),但这会忽略任何关联(如文档中所述)。在谷歌搜索后,我发现了使用FactoryGirl.build(:customer).attributes.symbolize_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
【问题讨论】:
标签: testing ruby-on-rails-4 associations factory-bot