mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Attributes without arguments/blocks look for a sequence
This commit is contained in:
parent
58ee31b91a
commit
3e08d6f159
3 changed files with 20 additions and 7 deletions
|
@ -56,7 +56,11 @@ module FactoryGirl
|
||||||
# are equivilent.
|
# are equivilent.
|
||||||
def method_missing(name, *args, &block)
|
def method_missing(name, *args, &block)
|
||||||
if args.empty? && block.nil?
|
if args.empty? && block.nil?
|
||||||
|
if sequence = FactoryGirl.sequences[name]
|
||||||
|
add_attribute(name) { sequence.next }
|
||||||
|
else
|
||||||
association(name)
|
association(name)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
add_attribute(name, *args, &block)
|
add_attribute(name, *args, &block)
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,6 +4,10 @@ require 'acceptance/acceptance_helper'
|
||||||
describe "integration" do
|
describe "integration" do
|
||||||
before do
|
before do
|
||||||
FactoryGirl.define do
|
FactoryGirl.define do
|
||||||
|
sequence :email do |n|
|
||||||
|
"somebody#{n}@example.com"
|
||||||
|
end
|
||||||
|
|
||||||
factory :user, :class => 'user' do
|
factory :user, :class => 'user' do
|
||||||
first_name 'Jimi'
|
first_name 'Jimi'
|
||||||
last_name 'Hendrix'
|
last_name 'Hendrix'
|
||||||
|
@ -26,8 +30,7 @@ describe "integration" do
|
||||||
last_name 'Stein'
|
last_name 'Stein'
|
||||||
admin true
|
admin true
|
||||||
sequence(:username) { |n| "username#{n}" }
|
sequence(:username) { |n| "username#{n}" }
|
||||||
# TODO: add sugar for this
|
email
|
||||||
email { Factory.next(:email) }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
factory :sequence_abuser, :class => User do
|
factory :sequence_abuser, :class => User do
|
||||||
|
@ -54,10 +57,6 @@ describe "integration" do
|
||||||
name 'Supplier of Awesome'
|
name 'Supplier of Awesome'
|
||||||
association :owner, :factory => :user
|
association :owner, :factory => :user
|
||||||
end
|
end
|
||||||
|
|
||||||
sequence :email do |n|
|
|
||||||
"somebody#{n}@example.com"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -120,6 +120,16 @@ describe FactoryGirl::DefinitionProxy do
|
||||||
factory.attributes.should include(attr)
|
factory.attributes.should include(attr)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "adds a sequence when passed an undefined method without arguments or a block" do
|
||||||
|
name = :airport
|
||||||
|
proxy = 'proxy'
|
||||||
|
FactoryGirl.sequences[name] = FactoryGirl::Sequence.new { |value| "expected" }
|
||||||
|
subject.send(name)
|
||||||
|
stub(proxy).set
|
||||||
|
factory.attributes.last.add_to(proxy)
|
||||||
|
proxy.should have_received.set(name, 'expected')
|
||||||
|
end
|
||||||
|
|
||||||
it "registers its factory for an alias" do
|
it "registers its factory for an alias" do
|
||||||
aliased_name = :guest
|
aliased_name = :guest
|
||||||
mock(FactoryGirl).register_factory(factory, :as => aliased_name)
|
mock(FactoryGirl).register_factory(factory, :as => aliased_name)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue