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.
|
||||
def method_missing(name, *args, &block)
|
||||
if args.empty? && block.nil?
|
||||
association(name)
|
||||
if sequence = FactoryGirl.sequences[name]
|
||||
add_attribute(name) { sequence.next }
|
||||
else
|
||||
association(name)
|
||||
end
|
||||
else
|
||||
add_attribute(name, *args, &block)
|
||||
end
|
||||
|
|
|
@ -4,6 +4,10 @@ require 'acceptance/acceptance_helper'
|
|||
describe "integration" do
|
||||
before do
|
||||
FactoryGirl.define do
|
||||
sequence :email do |n|
|
||||
"somebody#{n}@example.com"
|
||||
end
|
||||
|
||||
factory :user, :class => 'user' do
|
||||
first_name 'Jimi'
|
||||
last_name 'Hendrix'
|
||||
|
@ -26,8 +30,7 @@ describe "integration" do
|
|||
last_name 'Stein'
|
||||
admin true
|
||||
sequence(:username) { |n| "username#{n}" }
|
||||
# TODO: add sugar for this
|
||||
email { Factory.next(:email) }
|
||||
email
|
||||
end
|
||||
|
||||
factory :sequence_abuser, :class => User do
|
||||
|
@ -54,10 +57,6 @@ describe "integration" do
|
|||
name 'Supplier of Awesome'
|
||||
association :owner, :factory => :user
|
||||
end
|
||||
|
||||
sequence :email do |n|
|
||||
"somebody#{n}@example.com"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -120,6 +120,16 @@ describe FactoryGirl::DefinitionProxy do
|
|||
factory.attributes.should include(attr)
|
||||
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
|
||||
aliased_name = :guest
|
||||
mock(FactoryGirl).register_factory(factory, :as => aliased_name)
|
||||
|
|
Loading…
Reference in a new issue