anyone passed through this?
the scene is a nested form on user creation, where:
- User has_one Profile
- Profile belongs_to user
so my factory is configured in this way but when i run the test aways bring me this result:
Failure/Error: user_attributes[:user_attributes][:profile_attributes] = Factory.attributes_for :profile
NoMethodError:
undefined method `[]=' for nil:NilClass
Factory
Factory.define :user do |f|
f.after_build do |user|
f.email 'exemple@exemple.com'
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
Assuming that you are trying to auto create a profile when you create a user then try building it this way, using the new FactoryGirl syntax:
Factory file:
Note the addition of
userin the profile factory with defines the association on the profile record. Provided your user factory is called:useryou do not have to pass any arguments.You should then be able to call
and it will build both a user and a profile. You can see the profile by calling
@user.profile.If you call
@user = FactoryGirl.create(:user)it will create both the user and the profile, inserting theuser_idinto the profile record.