Permalink
Browse files

Split factories/sequences into separate registries most of the time;

Only use factories from step definitions
  • Loading branch information...
jferris committed May 19, 2011
1 parent 91d8614 commit c59981de1b2eb831dfd47a5df95e3ab71912c63b
Showing with 354 additions and 174 deletions.
  1. +1 −1 Rakefile
  2. +25 −18 features/support/factories.rb
  3. +2 −0 gemfiles/2.1.gemfile
  4. +4 −0 gemfiles/2.1.gemfile.lock
  5. +2 −0 gemfiles/2.3.gemfile
  6. +4 −0 gemfiles/2.3.gemfile.lock
  7. +2 −0 gemfiles/3.0.gemfile
  8. +4 −0 gemfiles/3.0.gemfile.lock
  9. +27 −0 lib/factory_girl.rb
  10. +4 −0 lib/factory_girl/attribute.rb
  11. +4 −0 lib/factory_girl/attribute/association.rb
  12. +36 −0 lib/factory_girl/attribute/implicit.rb
  13. +16 −0 lib/factory_girl/attribute/sequence.rb
  14. +2 −2 lib/factory_girl/definition_proxy.rb
  15. +1 −13 lib/factory_girl/factory.rb
  16. +0 −2 lib/factory_girl/find_definitions.rb
  17. +2 −2 lib/factory_girl/proxy/build.rb
  18. +2 −2 lib/factory_girl/proxy/stub.rb
  19. +7 −21 lib/factory_girl/registry.rb
  20. +3 −14 lib/factory_girl/sequence.rb
  21. +2 −2 lib/factory_girl/step_definitions.rb
  22. +1 −1 lib/factory_girl/syntax/blueprint.rb
  23. +3 −3 lib/factory_girl/syntax/default.rb
  24. +3 −3 lib/factory_girl/syntax/generate.rb
  25. +1 −1 lib/factory_girl/syntax/make.rb
  26. +15 −4 lib/factory_girl/syntax/methods.rb
  27. +2 −2 lib/factory_girl/syntax/sham.rb
  28. +31 −12 lib/factory_girl/syntax/vintage.rb
  29. +6 −4 spec/acceptance/sequence_spec.rb
  30. +3 −3 spec/acceptance/syntax/blueprint_spec.rb
  31. +4 −4 spec/acceptance/syntax/vintage_spec.rb
  32. +4 −0 spec/factory_girl/attribute/association_spec.rb
  33. +50 −0 spec/factory_girl/attribute/implicit_spec.rb
  34. +21 −0 spec/factory_girl/attribute/sequence_spec.rb
  35. +4 −0 spec/factory_girl/attribute_spec.rb
  36. +2 −13 spec/factory_girl/definition_proxy_spec.rb
  37. +4 −2 spec/factory_girl/factory_spec.rb
  38. +2 −2 spec/factory_girl/proxy/build_spec.rb
  39. +2 −2 spec/factory_girl/proxy/create_spec.rb
  40. +1 −1 spec/factory_girl/proxy/stub_spec.rb
  41. +11 −20 spec/factory_girl/registry_spec.rb
  42. +15 −19 spec/factory_girl/sequence_spec.rb
  43. +17 −0 spec/factory_girl_spec.rb
  44. +2 −1 spec/spec_helper.rb
View
@@ -17,7 +17,7 @@ end
namespace :spec do
desc "Run unit specs"
RSpec::Core::RakeTask.new('unit') do |t|
- t.pattern = 'spec/factory_girl/**/*_spec.rb'
+ t.pattern = 'spec/{*_spec.rb,factory_girl/**/*_spec.rb}'
end
desc "Run acceptance specs"
@@ -48,29 +48,36 @@ class Post < ActiveRecord::Base
class NonActiveRecord
end
-Factory.define :user do |f|
-end
+FactoryGirl.define do
+ # To make sure the step defs work with an email
+ sequence :email do |n|
+ "email#{n}@example.com"
+ end
-Factory.define :admin_user, :parent => :user do |f|
- f.admin true
-end
+ factory :user do
+ end
-Factory.define :category do |f|
- f.name "programming"
- f.association :category_group
-end
+ factory :admin_user, :parent => :user do
+ admin true
+ end
-Factory.define :category_group do |f|
- f.name "tecnhology"
-end
+ factory :category do
+ name "programming"
+ category_group
+ end
-Factory.define :post do |f|
- f.association :author, :factory => :user
- f.association :category
-end
+ factory :category_group do
+ name "tecnhology"
+ end
-# This is here to ensure that factory step definitions don't raise for a non-AR factory
-Factory.define :non_active_record do |f|
+ factory :post do
+ association :author, :factory => :user
+ category
+ end
+
+ # This is here to ensure that factory step definitions don't raise for a non-AR factory
+ factory :non_active_record do
+ end
end
require 'factory_girl/step_definitions'
View
@@ -5,7 +5,9 @@ gem "rr"
gem "activerecord", "~> 2.1"
gem "rake"
gem "sqlite3-ruby", {:require=>false}
+gem "bluecloth"
gem "rspec", "~> 2.0"
+gem "yard"
gem "rcov"
gem "cucumber"
gem "appraisal"
@@ -7,6 +7,7 @@ GEM
appraisal (0.1)
bundler
rake
+ bluecloth (2.1.0)
builder (2.1.2)
cucumber (0.9.4)
builder (~> 2.1.2)
@@ -32,16 +33,19 @@ GEM
rspec-mocks (2.1.0)
sqlite3-ruby (1.3.2)
term-ansicolor (1.0.5)
+ yard (0.7.1)
PLATFORMS
ruby
DEPENDENCIES
activerecord (~> 2.1)
appraisal
+ bluecloth
cucumber
rake
rcov
rr
rspec (~> 2.0)
sqlite3-ruby
+ yard
View
@@ -5,7 +5,9 @@ gem "rr"
gem "activerecord", "~> 2.3"
gem "rake"
gem "sqlite3-ruby", {:require=>false}
+gem "bluecloth"
gem "rspec", "~> 2.0"
+gem "yard"
gem "rcov"
gem "cucumber"
gem "appraisal"
@@ -7,6 +7,7 @@ GEM
appraisal (0.1)
bundler
rake
+ bluecloth (2.1.0)
builder (2.1.2)
cucumber (0.9.4)
builder (~> 2.1.2)
@@ -32,16 +33,19 @@ GEM
rspec-mocks (2.1.0)
sqlite3-ruby (1.3.2)
term-ansicolor (1.0.5)
+ yard (0.7.1)
PLATFORMS
ruby
DEPENDENCIES
activerecord (~> 2.3)
appraisal
+ bluecloth
cucumber
rake
rcov
rr
rspec (~> 2.0)
sqlite3-ruby
+ yard
View
@@ -5,7 +5,9 @@ gem "rr"
gem "activerecord", "~> 3.0"
gem "rake"
gem "sqlite3-ruby", {:require=>false}
+gem "bluecloth"
gem "rspec", "~> 2.0"
+gem "yard"
gem "rcov"
gem "cucumber"
gem "appraisal"
@@ -16,6 +16,7 @@ GEM
rake
arel (1.0.1)
activesupport (~> 3.0.0)
+ bluecloth (2.1.0)
builder (2.1.2)
cucumber (0.9.4)
builder (~> 2.1.2)
@@ -43,16 +44,19 @@ GEM
sqlite3-ruby (1.3.2)
term-ansicolor (1.0.5)
tzinfo (0.3.23)
+ yard (0.7.1)
PLATFORMS
ruby
DEPENDENCIES
activerecord (~> 3.0)
appraisal
+ bluecloth
cucumber
rake
rcov
rr
rspec (~> 2.0)
sqlite3-ruby
+ yard
View
@@ -10,6 +10,8 @@
require 'factory_girl/attribute/dynamic'
require 'factory_girl/attribute/association'
require 'factory_girl/attribute/callback'
+require 'factory_girl/attribute/sequence'
+require 'factory_girl/attribute/implicit'
require 'factory_girl/sequence'
require 'factory_girl/aliases'
require 'factory_girl/definition_proxy'
@@ -24,3 +26,28 @@
require 'factory_girl/rails2'
end
+module FactoryGirl
+ def self.factories
+ @factories ||= Registry.new
+ end
+
+ def self.register_factory(factory)
+ factories.add(factory)
+ end
+
+ def self.factory_by_name(name)
+ factories.find(name)
+ end
+
+ def self.sequences
+ @sequences ||= Registry.new
+ end
+
+ def self.register_sequence(sequence)
+ sequences.add(sequence)
+ end
+
+ def self.sequence_by_name(name)
+ sequences.find(name)
+ end
+end
@@ -24,6 +24,10 @@ def initialize(name)
def add_to(proxy)
end
+
+ def association?
+ false
+ end
end
end
@@ -14,6 +14,10 @@ def initialize(name, factory, overrides)
def add_to(proxy)
proxy.associate(name, @factory, @overrides)
end
+
+ def association?
+ true
+ end
end
end
@@ -0,0 +1,36 @@
+module FactoryGirl
+ class Attribute
+
+ class Implicit < Attribute
+ def initialize(name)
+ super(name)
+ end
+
+ def add_to(proxy)
+ implementation.add_to(proxy)
+ end
+
+ def association?
+ implementation.association?
+ end
+
+ def factory
+ name
+ end
+
+ private
+
+ def implementation
+ @implementation ||= resolve_name
+ end
+
+ def resolve_name
+ if FactoryGirl.factories.registered?(name)
+ Attribute::Association.new(name, name, {})
+ else
+ Attribute::Sequence.new(name, name)
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,16 @@
+module FactoryGirl
+ class Attribute
+
+ class Sequence < Attribute
+ def initialize(name, sequence)
+ super(name)
+ @sequence = sequence
+ end
+
+ def add_to(proxy)
+ proxy.set(name, FactoryGirl.generate(@sequence))
+ end
+ end
+
+ end
+end
@@ -73,7 +73,7 @@ def add_attribute(name, value = nil, &block)
# are equivilent.
def method_missing(name, *args, &block)
if args.empty? && block.nil?
- association(name)
+ @factory.define_attribute(Attribute::Implicit.new(name))
else
add_attribute(name, *args, &block)
end
@@ -97,7 +97,7 @@ def method_missing(name, *args, &block)
# Except that no globally available sequence will be defined.
def sequence(name, start_value = 1, &block)
sequence = Sequence.new(name, start_value, &block)
- add_attribute(name) { sequence.run }
+ add_attribute(name) { sequence.next }
end
# Adds an attribute that builds an association. The associated instance will
@@ -1,16 +1,4 @@
module FactoryGirl
- def self.factories
- puts "WARNING: FactoryGirl.factories is deprecated."
- puts "Use FactoryGirl.registry instead."
- registry
- end
-
- def self.factory_by_name(name)
- puts "WARNING: FactoryGirl.factory_by_name is deprecated."
- puts "Use FactoryGirl.find instead."
- registry.find(name)
- end
-
# Raised when a factory is defined that attempts to instantiate itself.
class AssociationDefinitionError < RuntimeError
end
@@ -101,7 +89,7 @@ def human_name(*args, &block)
end
def associations
- attributes.select {|attribute| attribute.is_a?(Attribute::Association) }
+ attributes.select {|attribute| attribute.association? }
end
# Names for this factory, including aliases.
@@ -1,8 +1,6 @@
module FactoryGirl
class << self
- attr_accessor :factories #:nodoc:
-
# An Array of strings specifying locations that should be searched for
# factory definitions. By default, factory_girl will attempt to require
# "factories," "test/factories," and "spec/factories." Only the first
@@ -14,12 +14,12 @@ def set(attribute, value)
end
def associate(name, factory_name, overrides)
- factory = FactoryGirl.find(factory_name)
+ factory = FactoryGirl.factory_by_name(factory_name)
set(name, factory.run(Proxy::Create, overrides))
end
def association(factory_name, overrides = {})
- factory = FactoryGirl.find(factory_name)
+ factory = FactoryGirl.factory_by_name(factory_name)
factory.run(Proxy::Create, overrides)
end
@@ -46,12 +46,12 @@ def set(attribute, value)
end
def associate(name, factory_name, overrides)
- factory = FactoryGirl.find(factory_name)
+ factory = FactoryGirl.factory_by_name(factory_name)
set(name, factory.run(Proxy::Stub, overrides))
end
def association(factory_name, overrides = {})
- factory = FactoryGirl.find(factory_name)
+ factory = FactoryGirl.factory_by_name(factory_name)
factory.run(Proxy::Stub, overrides)
end
Oops, something went wrong.

0 comments on commit c59981d

Please sign in to comment.