Skip to content

Commit

Permalink
Added in-line sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenebolshakov authored and jferris committed Feb 17, 2009
1 parent e4a3d25 commit 024e391
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.textile
Expand Up @@ -140,6 +140,19 @@ Factory.next :email
Factory.next :email
# => "person2@example.com"</code></pre>

Sequences can be used as lazy attributes:

Factory.define :user do |f|
f.email { Factory.next(:email) }
end

And it's also possible to define an in-line sequence that is only used in
a particular factory:

Factory.define :user do |f|
f.sequence(:username) { |n| "person#{n}" }
end


h2. Using factories

Expand Down
23 changes: 23 additions & 0 deletions lib/factory_girl/factory.rb
Expand Up @@ -131,6 +131,29 @@ def association (name, options = {})
end
@attributes << Attribute::Association.new(name, factory_name, options)
end

# Adds an attribute that will have unique values generated by a sequence with
# a specified format.
#
# The result of:
#
# Factory.define :user do |f|
# f.sequence(:email) { |n| "person#{n}@example.com" }
# end
#
# Is equal to:
#
# Factory.sequence(:email) { |n| "person#{n}@example.com" }
#
# Factory.define :user do |f|
# f.email { Factory.next(:email) }
# end
#
# Except that no globally available sequence will be defined
def sequence (name, &block)
s = Sequence.new(&block)
add_attribute(name) { s.next }
end

# Generates and returns a Hash of attributes from this factory. Attributes
# can be individually overridden by passing in a Hash of attribute => value
Expand Down
20 changes: 19 additions & 1 deletion test/factory_test.rb
Expand Up @@ -77,6 +77,24 @@ class FactoryTest < Test::Unit::TestCase
@factory.add_attribute(:name, 'value') {}
end
end

context "adding an attribute using a in-line sequence" do
should 'create the sequence' do
Factory::Sequence.
expects(:new)
@factory.sequence(:name) {}
end

should 'add a dynamic attribute' do
attr = mock('attribute', :name => :name)
Factory::Attribute::Dynamic.
expects(:new).
with(:name, instance_of(Proc)).
returns(attr)
@factory.sequence(:name) {}
assert @factory.attributes.include?(attr)
end
end

context "after adding an attribute" do
setup do
Expand Down Expand Up @@ -177,7 +195,7 @@ class FactoryTest < Test::Unit::TestCase
@factory.send(:name, 'value')
assert @factory.attributes.include?(attr)
end

context "when overriding generated attributes with a hash" do
setup do
@attr = :name
Expand Down
31 changes: 30 additions & 1 deletion test/integration_test.rb
Expand Up @@ -19,9 +19,10 @@ def setup
f.first_name 'Ben'
f.last_name 'Stein'
f.admin true
f.sequence(:username) { |n| "username#{n}" }
f.email { Factory.next(:email) }
end

Factory.sequence :email do |n|
"somebody#{n}@example.com"
end
Expand Down Expand Up @@ -169,5 +170,33 @@ def teardown
end

end

context "an attribute generated by an in-line sequence" do

setup do
@username = Factory.attributes_for(:admin)[:username]
end

should "match the correct format" do
assert_match /^username\d+$/, @username
end

context "after the attribute has already been generated once" do

setup do
@another_username = Factory.attributes_for(:admin)[:username]
end

should "match the correct format" do
assert_match /^username\d+$/, @username
end

should "not be the same as the first generated value" do
assert_not_equal @username, @another_username
end

end

end

end
1 change: 1 addition & 0 deletions test/models.rb
Expand Up @@ -9,6 +9,7 @@ def self.up
t.string :first_name
t.string :last_name
t.string :email
t.string :username
t.boolean :admin, :default => false
end

Expand Down

0 comments on commit 024e391

Please sign in to comment.