任何人都通過這個?配置工廠嵌套表格
場面上創建用戶,其中嵌套形式:
- 用戶HAS_ONE簡介
- 檔案belongs_to的用戶
所以我廠以這種方式配置,但是當我運行測試總是給我帶來這個結果:
Failure/Error: user_attributes[:user_attributes][:profile_attributes] = Factory.attributes_for :profile
NoMethodError:
undefined method `[]=' for nil:NilClass
廠
Factory.define :user do |f|
f.after_build do |user|
f.email '[email protected]'
f.password 'password'
f.password_confirmation 'password'
user.profile ||= Factory.build(:profile, :user => user)
end
end
Factory.define :profile do |f|
f.after_build do |profile|
profile.user ||= Factory.build(:user, :profile => profile)
f.nome 'alguem'
f.sobrenome 'alguem'
f.endereco 'rua x'
f.numero '95'
f.genero 'm'
f.complemento 'casa'
f.bairro 'bairro x'
f.cidade 'cidade x'
f.estado 'estado x'
f.cep '232323'
end
end
Users_spec
describe "CreateUsers" do
before :each do
user_attributes = Factory.attributes_for :user
user_attributes[:user_attributes][:profile_attributes] = Factory.attributes_for :profile
@user = User.new(user_attributes)
end
在嘗試分配user_attributes [:user_attributes] [:profile_attributes]之前,您可以嘗試提出user_attributes.inspect嗎?你的錯誤告訴你,user_attributes [:user_attributes]是零,所以當你試圖用[:profile_attributes]對它進行索引時,你正在使用一個方法,[] =,一個零值。 – 2012-02-17 20:29:51