Which is the correct method for aliasing attributes with FactoryGirl?
Well, I have the following in factories.rb
Factory.alias /(.*_)confirmation/, "\1"
Factory.define :user do |f|
f.new_pass 'asdasdasd'
f.new_pass_confirmation 'asdasdasd'
end
And then when I create th开发者_JAVA技巧e user I do the following:
Factory.build(:user, :new_pass => 'something', :new_pass_ => 'something_else')
But it throws me an error of:
undefined method `new_pass_=` for #<User:0x1234567>
Shouldn't FactoryGirl convert the new_pass_ to new_pass_confirmation?
Aliases in factory_girl are used to prevent two attributes from clashing with each other. The classic example is an association vs a foreign key: if your factory defines a "user" association and you override it by passing "user_id," the "user_id" should take precedence.
If you wanted the password confirmation to override password, you'd use this alias:
Factory.alias /(.*)_confirmation/, "\1"
It sounds like you want the password confirmation to default to the password, which you can do like this:
Factory.define :user do |f|
f.new_pass 'asdasdasd'
f.new_pass_confirmation { |u| u.new_pass }
end
In the new syntax, you can leave out the block arguments:
FactoryGirl.define do
factory :user do
new_pass 'asdasdasd'
new_pass_confirmation { new_pass }
end
end
精彩评论