mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Check arity to allow for Symbol#to_proc with dynamic attributes
Closes #698
This commit is contained in:
parent
3064f4a4ac
commit
930c21aed7
2 changed files with 37 additions and 1 deletions
|
@ -11,7 +11,10 @@ module FactoryGirl
|
|||
block = @block
|
||||
|
||||
-> {
|
||||
value = block.arity == 1 ? instance_exec(self, &block) : instance_exec(&block)
|
||||
value = case block.arity
|
||||
when 1, -1 then instance_exec(self, &block)
|
||||
else instance_exec(&block)
|
||||
end
|
||||
raise SequenceAbuseError if FactoryGirl::Sequence === value
|
||||
value
|
||||
}
|
||||
|
|
|
@ -19,3 +19,36 @@ describe "an instance generated by a factory with a custom class name" do
|
|||
it { should be_admin }
|
||||
end
|
||||
|
||||
describe "attributes defined using Symbol#to_proc" do
|
||||
before do
|
||||
define_model("User", password: :string, password_confirmation: :string)
|
||||
|
||||
FactoryGirl.define do
|
||||
factory :user do
|
||||
password "foo"
|
||||
password_confirmation &:password
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "assigns values correctly" do
|
||||
user = FactoryGirl.build(:user)
|
||||
|
||||
expect(user.password).to eq "foo"
|
||||
expect(user.password_confirmation).to eq "foo"
|
||||
end
|
||||
|
||||
it "assigns value with override correctly" do
|
||||
user = FactoryGirl.build(:user, password: "bar")
|
||||
|
||||
expect(user.password).to eq "bar"
|
||||
expect(user.password_confirmation).to eq "bar"
|
||||
end
|
||||
|
||||
it "assigns overridden value correctly" do
|
||||
user = FactoryGirl.build(:user, password_confirmation: "bar")
|
||||
|
||||
expect(user.password).to eq "foo"
|
||||
expect(user.password_confirmation).to eq "bar"
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue