Permalink
Browse files

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

Only use factories from step definitions
  • Loading branch information...
1 parent 91d8614 commit c59981de1b2eb831dfd47a5df95e3ab71912c63b @jferris jferris committed May 19, 2011
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
2 Rakefile
@@ -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"
View
43 features/support/factories.rb
@@ -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
2 gemfiles/2.1.gemfile
@@ -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"
View
4 gemfiles/2.1.gemfile.lock
@@ -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
2 gemfiles/2.3.gemfile
@@ -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"
View
4 gemfiles/2.3.gemfile.lock
@@ -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
2 gemfiles/3.0.gemfile
@@ -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"
View
4 gemfiles/3.0.gemfile.lock
@@ -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
27 lib/factory_girl.rb
@@ -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
View
4 lib/factory_girl/attribute.rb
@@ -24,6 +24,10 @@ def initialize(name)
def add_to(proxy)
end
+
+ def association?
+ false
+ end
end
end
View
4 lib/factory_girl/attribute/association.rb
@@ -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
View
36 lib/factory_girl/attribute/implicit.rb
@@ -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
View
16 lib/factory_girl/attribute/sequence.rb
@@ -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
View
4 lib/factory_girl/definition_proxy.rb
@@ -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
View
14 lib/factory_girl/factory.rb
@@ -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.
View
2 lib/factory_girl/find_definitions.rb
@@ -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
View
4 lib/factory_girl/proxy/build.rb
@@ -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
View
4 lib/factory_girl/proxy/stub.rb
@@ -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
View
28 lib/factory_girl/registry.rb
@@ -1,5 +1,7 @@
module FactoryGirl
class Registry
+ include Enumerable
+
def initialize
@items = {}
end
@@ -14,7 +16,7 @@ def find(name)
end
def each(&block)
- @items.each(&block)
+ @items.values.each(&block)
end
def [](name)
@@ -25,6 +27,10 @@ def registered?(name)
@items.key?(name.to_sym)
end
+ def clear
+ @items.clear
+ end
+
private
def add_as(name, item)
@@ -35,25 +41,5 @@ def add_as(name, item)
end
end
end
-
- def self.register(item)
- registry.add(item)
- end
-
- def self.registered?(name)
- registry.registered?(name)
- end
-
- def self.find(name)
- registry.find(name)
- end
-
- def self.registry
- @registry ||= Registry.new
- end
-
- def self.registry=(registry)
- @registry = registry
- end
end
View
17 lib/factory_girl/sequence.rb
@@ -6,25 +6,20 @@ class SequenceAbuseError < StandardError; end
# Sequences are defined using sequence within a FactoryGirl.define block.
# Sequence values are generated using next.
class Sequence
+ attr_reader :name
+
def initialize(name, value = 1, &proc) #:nodoc:
@name = name
@proc = proc
@value = value || 1
end
- # Returns the next value for this sequence
- def run(proxy_class = nil, overrides = {})
+ def next
@proc ? @proc.call(@value) : @value
ensure
@value = @value.next
end
- def next
- puts "WARNING: FactoryGirl::Sequence#next is deprecated."
- puts "Use #run instead."
- run
- end
-
def default_strategy
:create
end
@@ -33,10 +28,4 @@ def names
[@name]
end
end
-
- def self.sequences
- puts "WARNING: FactoryGirl.sequences is deprecated."
- puts "Use FactoryGirl.registry instead."
- registry
- end
end
View
4 lib/factory_girl/step_definitions.rb
@@ -3,7 +3,7 @@ module FactoryGirlStepHelpers
def convert_association_string_to_instance(factory_name, assignment)
attribute, value = assignment.split(':', 2)
return if value.blank?
- factory = FactoryGirl.find(factory_name)
+ factory = FactoryGirl.factory_by_name(factory_name)
attributes = convert_human_hash_to_attribute_hash({attribute => value.strip}, factory.associations)
attributes_find = {}
attributes.each do |k, v|
@@ -28,7 +28,7 @@ def convert_human_hash_to_attribute_hash(human_hash, associations = [])
World(FactoryGirlStepHelpers)
-FactoryGirl.registry.each do |name, factory|
+FactoryGirl.factories.each do |factory|
Given /^the following (?:#{factory.human_name}|#{factory.human_name.pluralize}) exists?:$/ do |table|
table.hashes.each do |human_hash|
attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
View
2 lib/factory_girl/syntax/blueprint.rb
@@ -29,7 +29,7 @@ def blueprint(&block)
instance = Factory.new(name.underscore, :class => self)
proxy = FactoryGirl::DefinitionProxy.new(instance)
proxy.instance_eval(&block)
- FactoryGirl.register(instance)
+ FactoryGirl.register_factory(instance)
end
end
View
6 lib/factory_girl/syntax/default.rb
@@ -17,13 +17,13 @@ def factory(name, options = {}, &block)
proxy = FactoryGirl::DefinitionProxy.new(factory)
proxy.instance_eval(&block)
if parent = options.delete(:parent)
- factory.inherit_from(FactoryGirl.find(parent))
+ factory.inherit_from(FactoryGirl.factory_by_name(parent))
end
- FactoryGirl.register(factory)
+ FactoryGirl.register_factory(factory)
end
def sequence(name, start_value = 1, &block)
- FactoryGirl.register(Sequence.new(name, start_value, &block))
+ FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
end
end
end
View
6 lib/factory_girl/syntax/generate.rb
@@ -42,22 +42,22 @@ def self.included(base) # :nodoc:
module ClassMethods #:nodoc:
def generate(overrides = {}, &block)
- factory = FactoryGirl.find(name.underscore)
+ factory = FactoryGirl.factory_by_name(name.underscore)
instance = factory.run(Proxy::Build, overrides)
instance.save
yield(instance) if block_given?
instance
end
def generate!(overrides = {}, &block)
- factory = FactoryGirl.find(name.underscore)
+ factory = FactoryGirl.factory_by_name(name.underscore)
instance = factory.run(Proxy::Create, overrides)
yield(instance) if block_given?
instance
end
def spawn(overrides = {}, &block)
- factory = FactoryGirl.find(name.underscore)
+ factory = FactoryGirl.factory_by_name(name.underscore)
instance = factory.run(Proxy::Build, overrides)
yield(instance) if block_given?
instance
View
2 lib/factory_girl/syntax/make.rb
@@ -28,7 +28,7 @@ def self.included(base) # :nodoc:
module ClassMethods #:nodoc:
def make(overrides = {})
- FactoryGirl.find(name.underscore).run(Proxy::Create, overrides)
+ FactoryGirl.factory_by_name(name.underscore).run(Proxy::Create, overrides)
end
end
View
19 lib/factory_girl/syntax/methods.rb
@@ -15,7 +15,7 @@ module Methods
# A set of attributes that can be used to build an instance of the class
# this factory generates.
def attributes_for(name, overrides = {})
- FactoryGirl.find(name).run(Proxy::AttributesFor, overrides)
+ FactoryGirl.factory_by_name(name).run(Proxy::AttributesFor, overrides)
end
# Generates and returns an instance from this factory. Attributes can be
@@ -31,7 +31,7 @@ def attributes_for(name, overrides = {})
# An instance of the class this factory generates, with generated attributes
# assigned.
def build(name, overrides = {})
- FactoryGirl.find(name).run(Proxy::Build, overrides)
+ FactoryGirl.factory_by_name(name).run(Proxy::Build, overrides)
end
# Generates, saves, and returns an instance from this factory. Attributes can
@@ -51,7 +51,7 @@ def build(name, overrides = {})
# A saved instance of the class this factory generates, with generated
# attributes assigned.
def create(name, overrides = {})
- FactoryGirl.find(name).run(Proxy::Create, overrides)
+ FactoryGirl.factory_by_name(name).run(Proxy::Create, overrides)
end
# Generates and returns an object with all attributes from this factory
@@ -67,9 +67,20 @@ def create(name, overrides = {})
# Returns: +Object+
# An object with generated attributes stubbed out.
def build_stubbed(name, overrides = {})
- FactoryGirl.find(name).run(Proxy::Stub, overrides)
+ FactoryGirl.factory_by_name(name).run(Proxy::Stub, overrides)
end
+ # Generates and returns the next value in a sequence.
+ #
+ # Arguments:
+ # name: (Symbol)
+ # The name of the sequence that a value should be generated for.
+ #
+ # Returns:
+ # The next value in the sequence. (Object)
+ def generate(name)
+ FactoryGirl.sequence_by_name(name).next
+ end
end
end
end
View
4 lib/factory_girl/syntax/sham.rb
@@ -27,9 +27,9 @@ module Sham #:nodoc:
def self.method_missing(name, *args, &block)
if block_given?
start_value = args.first
- FactoryGirl.register(Sequence.new(name, start_value || 1, &block))
+ FactoryGirl.register_sequence(Sequence.new(name, start_value || 1, &block))
else
- FactoryGirl.find(name).run
+ FactoryGirl.sequence_by_name(name).next
end
end
View
43 lib/factory_girl/syntax/vintage.rb
@@ -2,8 +2,6 @@ module FactoryGirl
module Syntax
module Vintage
module Factory
- extend Syntax::Methods
-
# Defines a new factory that can be used by the build strategies (create and
# build) to build new objects.
#
@@ -31,14 +29,18 @@ def self.define(name, options = {})
proxy = FactoryGirl::DefinitionProxy.new(factory)
yield(proxy)
if parent = options.delete(:parent)
- factory.inherit_from(FactoryGirl.find(parent))
+ factory.inherit_from(FactoryGirl.factory_by_name(parent))
end
- FactoryGirl.register(factory)
+ FactoryGirl.register_factory(factory)
end
# Executes the default strategy for the given factory. This is usually create,
# but it can be overridden for each factory.
#
+ # DEPRECATED
+ #
+ # Use create instead.
+ #
# Arguments:
# * name: +Symbol+ or +String+
# The name of the factory that should be used.
@@ -48,7 +50,7 @@ def self.define(name, options = {})
# Returns: +Object+
# The result of the default strategy.
def self.default_strategy(name, overrides = {})
- self.send(FactoryGirl.find(name).default_strategy, name, overrides)
+ FactoryGirl.send(FactoryGirl.factory_by_name(name).default_strategy, name, overrides)
end
# Defines a new sequence that can be used to generate unique values in a specific format.
@@ -67,7 +69,7 @@ def self.default_strategy(name, overrides = {})
#
# Factory.sequence(:email) {|n| "somebody_#{n}@example.com" }
def self.sequence(name, start_value = 1, &block)
- FactoryGirl.register(Sequence.new(name, start_value, &block))
+ FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
end
# Generates and returns the next value in a sequence.
@@ -76,12 +78,10 @@ def self.sequence(name, start_value = 1, &block)
# name: (Symbol)
# The name of the sequence that a value should be generated for.
#
- # DEPRECATED. Use create instead.
- #
# Returns:
# The next value in the sequence. (Object)
def self.next(name)
- FactoryGirl.find(name).next
+ FactoryGirl.generate(name)
end
# Defines a new alias for attributes.
@@ -112,15 +112,34 @@ def self.alias(pattern, replace)
FactoryGirl.aliases << [pattern, replace]
end
- # Alias for build_stubbed. Deprecated.
- def self.stub(name, overrides = {})
- build_stubbed(name, overrides)
+ # Alias for FactoryGirl.attributes_for
+ def self.attributes_for(name, overrides = {})
+ FactoryGirl.attributes_for(name, overrides)
+ end
+
+ # Alias for FactoryGirl.build
+ def self.build(name, overrides = {})
+ FactoryGirl.build(name, overrides)
end
+ # Alias for FactoryGirl.create
+ def self.create(name, overrides = {})
+ FactoryGirl.create(name, overrides)
+ end
+
+ # Alias for FactoryGirl.build_stubbed.
+ def self.stub(name, overrides = {})
+ FactoryGirl.build_stubbed(name, overrides)
+ end
end
# Shortcut for Factory.default_strategy.
#
+ # DEPRECATION WARNING:
+ #
+ # In a future release, default_strategy will be removed and this will
+ # simply call create instead.
+ #
# Example:
# Factory(:user, :name => 'Joe')
def Factory(name, attrs = {})
View
10 spec/acceptance/sequence_spec.rb
@@ -2,15 +2,17 @@
require 'acceptance/acceptance_helper'
describe "sequences" do
+ include FactoryGirl::Syntax::Methods
+
it "generates several values in the correct format" do
FactoryGirl.define do
sequence :email do |n|
"somebody#{n}@example.com"
end
end
- first_value = FactoryGirl.create(:email)
- another_value = FactoryGirl.create(:email)
+ first_value = generate(:email)
+ another_value = generate(:email)
first_value.should =~ /^somebody\d+@example\.com$/
another_value.should =~ /^somebody\d+@example\.com$/
@@ -22,8 +24,8 @@
sequence :order
end
- first_value = FactoryGirl.create(:order)
- another_value = FactoryGirl.create(:order)
+ first_value = generate(:order)
+ another_value = generate(:order)
first_value.should == 1
another_value.should == 2
View
6 spec/acceptance/syntax/blueprint_spec.rb
@@ -9,9 +9,9 @@
Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
User.blueprint do
- first_name { 'Bill' }
- last_name { 'Nye' }
- email { FactoryGirl.create(:email) }
+ first_name { 'Bill' }
+ last_name { 'Nye' }
+ email { FactoryGirl.generate(:email) }
end
end
View
8 spec/acceptance/syntax/vintage_spec.rb
@@ -11,7 +11,7 @@
Factory.define :user do |factory|
factory.first_name { 'Bill' }
factory.last_name { 'Nye' }
- factory.email { Factory(:email) }
+ factory.email { Factory.next(:email) }
end
end
@@ -51,7 +51,7 @@
before do
@parent = FactoryGirl::Factory.new(:object)
@parent.define_attribute(FactoryGirl::Attribute::Static.new(:name, 'value'))
- FactoryGirl.register(@parent)
+ FactoryGirl.register_factory(@parent)
end
it "should raise an ArgumentError when trying to use a non-existent factory as parent" do
@@ -87,7 +87,7 @@
it "should add the factory to the list of factories" do
Factory.define(@name) {|f| }
- @factory.should == FactoryGirl.find(@name)
+ @factory.should == FactoryGirl.factory_by_name(@name)
end
end
@@ -96,7 +96,7 @@
@name = :user
@factory = FactoryGirl::Factory.new(@name)
- FactoryGirl.register(@factory)
+ FactoryGirl.register_factory(@factory)
end
it "should use Proxy::AttributesFor for Factory.attributes_for" do
View
4 spec/factory_girl/attribute/association_spec.rb
@@ -12,6 +12,10 @@
@attr.name.should == @name
end
+ it "is an association" do
+ @attr.should be_association
+ end
+
it "should have a factory" do
@attr.factory.should == @factory
end
View
50 spec/factory_girl/attribute/implicit_spec.rb
@@ -0,0 +1,50 @@
+require 'spec_helper'
+
+describe FactoryGirl::Attribute::Implicit do
+ before do
+ @name = :author
+ @attr = FactoryGirl::Attribute::Implicit.new(@name)
+ end
+
+ it "has a name" do
+ @attr.name.should == @name
+ end
+
+ context "with a known factory" do
+ before do
+ stub(FactoryGirl.factories).registered? { true }
+ end
+
+ it "associates the factory" do
+ proxy = "proxy"
+ stub(proxy).associate
+ @attr.add_to(proxy)
+ proxy.should have_received.associate(@name, @name, {})
+ end
+
+ it "is an association" do
+ @attr.should be_association
+ end
+
+ it "has a factory" do
+ @attr.factory.should == @name
+ end
+ end
+
+ context "with a known sequence" do
+ before do
+ FactoryGirl.register_sequence(FactoryGirl::Sequence.new(@name, 1) { "magic" })
+ end
+
+ it "generates the sequence" do
+ proxy = "proxy"
+ stub(proxy).set
+ @attr.add_to(proxy)
+ proxy.should have_received.set(@name, "magic")
+ end
+
+ it "isn't an association" do
+ @attr.should_not be_association
+ end
+ end
+end
View
21 spec/factory_girl/attribute/sequence_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+describe FactoryGirl::Attribute::Sequence do
+ before do
+ @name = :first_name
+ @sequence = :name
+ FactoryGirl.register_sequence(FactoryGirl::Sequence.new(@sequence, 5) { |n| "Name #{n}" })
+ @attr = FactoryGirl::Attribute::Sequence.new(@name, @sequence)
+ end
+
+ it "should have a name" do
+ @attr.name.should == @name
+ end
+
+ it "assigns the next value in the sequence" do
+ proxy = "proxy"
+ stub(proxy).set
+ @attr.add_to(proxy)
+ proxy.should have_received.set(@name, "Name 5")
+ end
+end
View
4 spec/factory_girl/attribute_spec.rb
@@ -10,6 +10,10 @@
@attr.name.should == @name
end
+ it "isn't an association" do
+ @attr.should_not be_association
+ end
+
it "should do nothing when being added to a proxy" do
@proxy = "proxy"
stub(@proxy).set
View
15 spec/factory_girl/definition_proxy_spec.rb
@@ -111,22 +111,11 @@
factory.attributes.should include(attribute)
end
- it "adds an association when passed an undefined method without arguments or a block" do
+ it "adds an implicit attribute when passed an undefined method without arguments or a block" do
name = :user
- FactoryGirl.register(FactoryGirl::Factory.new(name))
attr = 'attribute'
stub(attr).name { name }
- mock(FactoryGirl::Attribute::Association).new(name, name, {}) { attr }
- subject.send(name)
- factory.attributes.should include(attr)
- end
-
- it "adds a sequence when passed an undefined method without arguments or a block" do
- name = :airport
- FactoryGirl.register(FactoryGirl::Sequence.new(name))
- attr = 'attribute'
- stub(attr).name { name }
- mock(FactoryGirl::Attribute::Association).new(name, name, {}) { attr }
+ mock(FactoryGirl::Attribute::Implicit).new(name) { attr }
subject.send(name)
factory.attributes.should include(attr)
end
View
6 spec/factory_girl/factory_spec.rb
@@ -89,12 +89,14 @@
it "should return associations" do
factory = FactoryGirl::Factory.new(:post)
+ FactoryGirl.register_factory(FactoryGirl::Factory.new(:admin))
factory.define_attribute(FactoryGirl::Attribute::Association.new(:author, :author, {}))
factory.define_attribute(FactoryGirl::Attribute::Association.new(:editor, :editor, {}))
+ factory.define_attribute(FactoryGirl::Attribute::Implicit.new(:admin))
factory.associations.each do |association|
- association.should be_a(FactoryGirl::Attribute::Association)
+ association.should be_association
end
- factory.associations.size.should == 2
+ factory.associations.size.should == 3
end
it "should raise for a self referencing association" do
View
4 spec/factory_girl/proxy/build_spec.rb
@@ -21,7 +21,7 @@
before do
@association = "associated-instance"
@associated_factory = "associated-factory"
- stub(FactoryGirl).find { @associated_factory }
+ stub(FactoryGirl).factory_by_name { @associated_factory }
stub(@associated_factory).run { @association }
@overrides = { 'attr' => 'value' }
@proxy.associate(:owner, :user, @overrides)
@@ -39,7 +39,7 @@
it "should run create when building an association" do
association = "associated-instance"
associated_factory = "associated-factory"
- stub(FactoryGirl).find { associated_factory }
+ stub(FactoryGirl).factory_by_name { associated_factory }
stub(associated_factory).run { association }
overrides = { 'attr' => 'value' }
@proxy.association(:user, overrides).should == association
View
4 spec/factory_girl/proxy/create_spec.rb
@@ -22,7 +22,7 @@
before do
@association = "associated-instance"
@associated_factory = "associated-factory"
- stub(FactoryGirl).find { @associated_factory }
+ stub(FactoryGirl).factory_by_name { @associated_factory }
stub(@associated_factory).run { @association }
@overrides = { 'attr' => 'value' }
@proxy.associate(:owner, :user, @overrides)
@@ -40,7 +40,7 @@
it "should run create when building an association" do
association = "associated-instance"
associated_factory = "associated-factory"
- stub(FactoryGirl).find { associated_factory }
+ stub(FactoryGirl).factory_by_name { associated_factory }
stub(associated_factory).run { association }
overrides = { 'attr' => 'value' }
@proxy.association(:user, overrides).should == association
View
2 spec/factory_girl/proxy/stub_spec.rb
@@ -23,7 +23,7 @@
describe "when a user factory exists" do
before do
@user = "user"
- stub(FactoryGirl).find { @associated_factory }
+ stub(FactoryGirl).factory_by_name { @associated_factory }
@associated_factory = 'associate-factory'
end
View
31 spec/factory_girl/registry_spec.rb
@@ -41,14 +41,13 @@
other_factory = FactoryGirl::Factory.new(:string)
subject.add(factory)
subject.add(other_factory)
- result = {}
+ result = []
- subject.each do |name, value|
- result[name] = value
+ subject.each do |value|
+ result << value
end
- result.should == { factory.name => factory,
- other_factory.name => other_factory }
+ result.should =~ [factory, other_factory]
end
it "registers an sequence" do
@@ -71,22 +70,14 @@
end
end
- context "on the FactoryGirl module" do
- it "finds a registered a factory" do
- FactoryGirl.register(factory)
- FactoryGirl.find(factory.name).should == factory
- end
-
- it "knows that a factory is registered by symbol" do
- FactoryGirl.register(factory)
- FactoryGirl.should be_registered(factory.name.to_sym)
- end
+ it "is enumerable" do
+ should be_kind_of(Enumerable)
+ end
- it "sets the registry" do
- registry = FactoryGirl::Registry.new
- FactoryGirl.registry = registry
- FactoryGirl.registry.should == registry
- end
+ it "clears registered factories" do
+ subject.add(factory)
+ subject.clear
+ subject.count.should == 0
end
end
View
34 spec/factory_girl/sequence_spec.rb
@@ -7,33 +7,29 @@
@sequence = FactoryGirl::Sequence.new(@name) {|n| "=#{n}" }
end
+ it "has a name" do
+ @sequence.name.should == @name
+ end
+
it "has names" do
@sequence.names.should == [@name]
end
it "should start with a value of 1" do
- @sequence.run.should == "=1"
- end
-
- it "responds to next" do
@sequence.next.should == "=1"
end
it "responds to default_strategy" do
@sequence.default_strategy.should == :create
end
- it "runs compatible with the Factory interface" do
- @sequence.run(nil, nil).should == "=1"
- end
-
describe "after being called" do
before do
- @sequence.run
+ @sequence.next
end
it "should use the next value" do
- @sequence.run.should == "=2"
+ @sequence.next.should == "=2"
end
end
end
@@ -44,16 +40,16 @@
end
it "should start with a value of A" do
- @sequence.run.should == "=A"
+ @sequence.next.should == "=A"
end
describe "after being called" do
before do
- @sequence.run
+ @sequence.next
end
it "should use the next value" do
- @sequence.run.should == "=B"
+ @sequence.next.should == "=B"
end
end
end
@@ -64,16 +60,16 @@
end
it "should start with a value of 1" do
- @sequence.run.should == 1
+ @sequence.next.should == 1
end
describe "after being called" do
before do
- @sequence.run
+ @sequence.next
end
it "should use the next value" do
- @sequence.run.should == 2
+ @sequence.next.should == 2
end
end
end
@@ -84,16 +80,16 @@
end
it "should start with a value of A" do
- @sequence.run.should == "A"
+ @sequence.next.should == "A"
end
describe "after being called" do
before do
- @sequence.run
+ @sequence.next
end
it "should use the next value" do
- @sequence.run.should == "B"
+ @sequence.next.should == "B"
end
end
end
View
17 spec/factory_girl_spec.rb
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+describe FactoryGirl do
+ let(:factory) { FactoryGirl::Factory.new(:object) }
+ let(:sequence) { FactoryGirl::Sequence.new(:email) }
+
+ it "finds a registered a factory" do
+ FactoryGirl.register_factory(factory)
+ FactoryGirl.factory_by_name(factory.name).should == factory
+ end
+
+ it "finds a registered a sequence" do
+ FactoryGirl.register_sequence(sequence)
+ FactoryGirl.sequence_by_name(sequence.name).should == sequence
+ end
+end
+
View
3 spec/spec_helper.rb
@@ -22,7 +22,8 @@ def self.included(mod)
config.mock_framework = :rr
RSpec::Core::ExampleGroup.send(:include, RR::Adapters::Rspec)
config.after do
- FactoryGirl.registry = FactoryGirl::Registry.new
+ FactoryGirl.factories.clear
+ FactoryGirl.sequences.clear
end
end

0 comments on commit c59981d

Please sign in to comment.