factory_girl_kibiz0r 2.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- data/Appraisals +12 -0
- data/CONTRIBUTION_GUIDELINES.md +9 -0
- data/Changelog +29 -0
- data/GETTING_STARTED.md +246 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +62 -0
- data/LICENSE +19 -0
- data/README.md +64 -0
- data/Rakefile +54 -0
- data/features/factory_girl_steps.feature +151 -0
- data/features/step_definitions/database_steps.rb +20 -0
- data/features/support/env.rb +6 -0
- data/features/support/factories.rb +84 -0
- data/lib/factory_girl/aliases.rb +21 -0
- data/lib/factory_girl/attribute/association.rb +24 -0
- data/lib/factory_girl/attribute/callback.rb +16 -0
- data/lib/factory_girl/attribute/dynamic.rb +21 -0
- data/lib/factory_girl/attribute/implicit.rb +36 -0
- data/lib/factory_girl/attribute/list.rb +19 -0
- data/lib/factory_girl/attribute/sequence.rb +16 -0
- data/lib/factory_girl/attribute/static.rb +18 -0
- data/lib/factory_girl/attribute.rb +42 -0
- data/lib/factory_girl/definition_proxy.rb +147 -0
- data/lib/factory_girl/deprecated.rb +18 -0
- data/lib/factory_girl/factory.rb +196 -0
- data/lib/factory_girl/find_definitions.rb +23 -0
- data/lib/factory_girl/proxy/attributes_for.rb +21 -0
- data/lib/factory_girl/proxy/build.rb +36 -0
- data/lib/factory_girl/proxy/create.rb +16 -0
- data/lib/factory_girl/proxy/stub.rb +64 -0
- data/lib/factory_girl/proxy.rb +87 -0
- data/lib/factory_girl/rails2.rb +1 -0
- data/lib/factory_girl/registry.rb +45 -0
- data/lib/factory_girl/sequence.rb +31 -0
- data/lib/factory_girl/step_definitions.rb +60 -0
- data/lib/factory_girl/syntax/blueprint.rb +42 -0
- data/lib/factory_girl/syntax/default.rb +33 -0
- data/lib/factory_girl/syntax/generate.rb +73 -0
- data/lib/factory_girl/syntax/make.rb +41 -0
- data/lib/factory_girl/syntax/methods.rb +86 -0
- data/lib/factory_girl/syntax/sham.rb +45 -0
- data/lib/factory_girl/syntax/vintage.rb +152 -0
- data/lib/factory_girl/syntax.rb +12 -0
- data/lib/factory_girl/version.rb +4 -0
- data/lib/factory_girl.rb +54 -0
- data/spec/acceptance/acceptance_helper.rb +11 -0
- data/spec/acceptance/attribute_aliases_spec.rb +26 -0
- data/spec/acceptance/attributes_for_spec.rb +48 -0
- data/spec/acceptance/build_spec.rb +35 -0
- data/spec/acceptance/build_stubbed_spec.rb +79 -0
- data/spec/acceptance/callbacks_spec.rb +53 -0
- data/spec/acceptance/create_spec.rb +67 -0
- data/spec/acceptance/default_strategy_spec.rb +27 -0
- data/spec/acceptance/definition_spec.rb +28 -0
- data/spec/acceptance/parent_spec.rb +39 -0
- data/spec/acceptance/sequence_spec.rb +34 -0
- data/spec/acceptance/syntax/blueprint_spec.rb +32 -0
- data/spec/acceptance/syntax/generate_spec.rb +60 -0
- data/spec/acceptance/syntax/make_spec.rb +35 -0
- data/spec/acceptance/syntax/sham_spec.rb +44 -0
- data/spec/acceptance/syntax/vintage_spec.rb +224 -0
- data/spec/factory_girl/aliases_spec.rb +33 -0
- data/spec/factory_girl/attribute/association_spec.rb +33 -0
- data/spec/factory_girl/attribute/callback_spec.rb +23 -0
- data/spec/factory_girl/attribute/dynamic_spec.rb +60 -0
- data/spec/factory_girl/attribute/implicit_spec.rb +50 -0
- data/spec/factory_girl/attribute/sequence_spec.rb +21 -0
- data/spec/factory_girl/attribute/static_spec.rb +29 -0
- data/spec/factory_girl/attribute_spec.rb +39 -0
- data/spec/factory_girl/definition_proxy_spec.rb +129 -0
- data/spec/factory_girl/deprecated_spec.rb +66 -0
- data/spec/factory_girl/factory_spec.rb +374 -0
- data/spec/factory_girl/find_definitions_spec.rb +100 -0
- data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
- data/spec/factory_girl/proxy/build_spec.rb +86 -0
- data/spec/factory_girl/proxy/create_spec.rb +107 -0
- data/spec/factory_girl/proxy/stub_spec.rb +80 -0
- data/spec/factory_girl/proxy_spec.rb +102 -0
- data/spec/factory_girl/registry_spec.rb +83 -0
- data/spec/factory_girl/sequence_spec.rb +96 -0
- data/spec/factory_girl_spec.rb +17 -0
- data/spec/spec_helper.rb +93 -0
- metadata +294 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Proxy #:nodoc:
|
3
|
+
class AttributesFor < Proxy #:nodoc:
|
4
|
+
def initialize(klass)
|
5
|
+
@hash = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def get(attribute)
|
9
|
+
@hash[attribute]
|
10
|
+
end
|
11
|
+
|
12
|
+
def set(attribute, value)
|
13
|
+
@hash[attribute] = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def result(to_create)
|
17
|
+
@hash
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Proxy #:nodoc:
|
3
|
+
class Build < Proxy #:nodoc:
|
4
|
+
def initialize(klass)
|
5
|
+
@instance = klass.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def get(attribute)
|
9
|
+
if @instance.respond_to?(attribute)
|
10
|
+
@instance.send(attribute)
|
11
|
+
else
|
12
|
+
super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def set(attribute, value)
|
17
|
+
@instance.send(:"#{attribute}=", value)
|
18
|
+
end
|
19
|
+
|
20
|
+
def associate(name, factory_name, overrides)
|
21
|
+
factory = FactoryGirl.factory_by_name(factory_name)
|
22
|
+
set(name, factory.run(Proxy::Create, overrides))
|
23
|
+
end
|
24
|
+
|
25
|
+
def association(factory_name, overrides = {})
|
26
|
+
factory = FactoryGirl.factory_by_name(factory_name)
|
27
|
+
factory.run(Proxy::Create, overrides)
|
28
|
+
end
|
29
|
+
|
30
|
+
def result(to_create)
|
31
|
+
run_callbacks(:after_build)
|
32
|
+
@instance
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Proxy #:nodoc:
|
3
|
+
class Create < Build #:nodoc:
|
4
|
+
def result(to_create)
|
5
|
+
run_callbacks(:after_build)
|
6
|
+
if to_create
|
7
|
+
to_create.call(@instance)
|
8
|
+
else
|
9
|
+
@instance.save!
|
10
|
+
end
|
11
|
+
run_callbacks(:after_create)
|
12
|
+
@instance
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Proxy
|
3
|
+
class Stub < Proxy #:nodoc:
|
4
|
+
@@next_id = 1000
|
5
|
+
|
6
|
+
def initialize(klass)
|
7
|
+
@instance = klass.new
|
8
|
+
@instance.id = next_id
|
9
|
+
@instance.instance_eval do
|
10
|
+
def new_record?
|
11
|
+
id.nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
def save(*args)
|
15
|
+
raise "stubbed models are not allowed to access the database"
|
16
|
+
end
|
17
|
+
|
18
|
+
def destroy(*args)
|
19
|
+
raise "stubbed models are not allowed to access the database"
|
20
|
+
end
|
21
|
+
|
22
|
+
def connection
|
23
|
+
raise "stubbed models are not allowed to access the database"
|
24
|
+
end
|
25
|
+
|
26
|
+
def reload
|
27
|
+
raise "stubbed models are not allowed to access the database"
|
28
|
+
end
|
29
|
+
|
30
|
+
def update_attribute(*args)
|
31
|
+
raise "stubbed models are not allowed to access the database"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def next_id
|
37
|
+
@@next_id += 1
|
38
|
+
end
|
39
|
+
|
40
|
+
def get(attribute)
|
41
|
+
@instance.send(attribute)
|
42
|
+
end
|
43
|
+
|
44
|
+
def set(attribute, value)
|
45
|
+
@instance.send(:"#{attribute}=", value)
|
46
|
+
end
|
47
|
+
|
48
|
+
def associate(name, factory_name, overrides)
|
49
|
+
factory = FactoryGirl.factory_by_name(factory_name)
|
50
|
+
set(name, factory.run(Proxy::Stub, overrides))
|
51
|
+
end
|
52
|
+
|
53
|
+
def association(factory_name, overrides = {})
|
54
|
+
factory = FactoryGirl.factory_by_name(factory_name)
|
55
|
+
factory.run(Proxy::Stub, overrides)
|
56
|
+
end
|
57
|
+
|
58
|
+
def result(to_create)
|
59
|
+
run_callbacks(:after_stub)
|
60
|
+
@instance
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Proxy #:nodoc:
|
3
|
+
|
4
|
+
attr_reader :callbacks
|
5
|
+
|
6
|
+
def initialize(klass)
|
7
|
+
end
|
8
|
+
|
9
|
+
def get(attribute)
|
10
|
+
@ignored && @ignored[attribute]
|
11
|
+
end
|
12
|
+
|
13
|
+
def set(attribute, value)
|
14
|
+
end
|
15
|
+
|
16
|
+
def ignore(attribute, value)
|
17
|
+
@ignored = {}
|
18
|
+
@ignored[attribute] = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def associate(name, factory, attributes)
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_callback(name, block)
|
25
|
+
@callbacks ||= {}
|
26
|
+
@callbacks[name] ||= []
|
27
|
+
@callbacks[name] << block
|
28
|
+
end
|
29
|
+
|
30
|
+
def run_callbacks(name)
|
31
|
+
if @callbacks && @callbacks[name]
|
32
|
+
@callbacks[name].each do |block|
|
33
|
+
case block.arity
|
34
|
+
when 0 then block.call
|
35
|
+
when 2 then block.call(@instance, self)
|
36
|
+
else
|
37
|
+
block.call(@instance)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Generates an association using the current build strategy.
|
44
|
+
#
|
45
|
+
# Arguments:
|
46
|
+
# name: (Symbol)
|
47
|
+
# The name of the factory that should be used to generate this
|
48
|
+
# association.
|
49
|
+
# attributes: (Hash)
|
50
|
+
# A hash of attributes that should be overridden for this association.
|
51
|
+
#
|
52
|
+
# Returns:
|
53
|
+
# The generated association for the current build strategy. Note that
|
54
|
+
# associations are not generated for the attributes_for strategy. Returns
|
55
|
+
# nil in this case.
|
56
|
+
#
|
57
|
+
# Example:
|
58
|
+
#
|
59
|
+
# factory :user do
|
60
|
+
# # ...
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# factory :post do
|
64
|
+
# # ...
|
65
|
+
# author { |post| post.association(:user, :name => 'Joe') }
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# # Builds (but doesn't save) a Post and a User
|
69
|
+
# FactoryGirl.build(:post)
|
70
|
+
#
|
71
|
+
# # Builds and saves a User, builds a Post, assigns the User to the
|
72
|
+
# # author association, and saves the User.
|
73
|
+
# FactoryGirl.create(:post)
|
74
|
+
#
|
75
|
+
def association(name, overrides = {})
|
76
|
+
nil
|
77
|
+
end
|
78
|
+
|
79
|
+
def method_missing(method, *args, &block)
|
80
|
+
get(method)
|
81
|
+
end
|
82
|
+
|
83
|
+
def result(to_create)
|
84
|
+
raise NotImplementedError, "Strategies must return a result"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Rails.configuration.after_initialize { FactoryGirl.find_definitions }
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Registry
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@items = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def add(item)
|
10
|
+
item.names.each { |name| add_as(name, item) }
|
11
|
+
item
|
12
|
+
end
|
13
|
+
|
14
|
+
def find(name)
|
15
|
+
@items[name.to_sym] or raise ArgumentError.new("Not registered: #{name.to_s}")
|
16
|
+
end
|
17
|
+
|
18
|
+
def each(&block)
|
19
|
+
@items.values.each(&block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def [](name)
|
23
|
+
find(name)
|
24
|
+
end
|
25
|
+
|
26
|
+
def registered?(name)
|
27
|
+
@items.key?(name.to_sym)
|
28
|
+
end
|
29
|
+
|
30
|
+
def clear
|
31
|
+
@items.clear
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def add_as(name, item)
|
37
|
+
if registered?(name)
|
38
|
+
raise DuplicateDefinitionError, "Already defined: #{name}"
|
39
|
+
else
|
40
|
+
@items[name.to_sym] = item
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
|
3
|
+
# Raised when calling Factory.sequence from a dynamic attribute block
|
4
|
+
class SequenceAbuseError < StandardError; end
|
5
|
+
|
6
|
+
# Sequences are defined using sequence within a FactoryGirl.define block.
|
7
|
+
# Sequence values are generated using next.
|
8
|
+
class Sequence
|
9
|
+
attr_reader :name
|
10
|
+
|
11
|
+
def initialize(name, value = 1, &proc) #:nodoc:
|
12
|
+
@name = name
|
13
|
+
@proc = proc
|
14
|
+
@value = value || 1
|
15
|
+
end
|
16
|
+
|
17
|
+
def next
|
18
|
+
@proc ? @proc.call(@value) : @value
|
19
|
+
ensure
|
20
|
+
@value = @value.next
|
21
|
+
end
|
22
|
+
|
23
|
+
def default_strategy
|
24
|
+
:create
|
25
|
+
end
|
26
|
+
|
27
|
+
def names
|
28
|
+
[@name]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module FactoryGirlStepHelpers
|
2
|
+
|
3
|
+
def convert_association_string_to_instance(factory_name, assignment)
|
4
|
+
attribute, value = assignment.split(':', 2)
|
5
|
+
return if value.blank?
|
6
|
+
factory = FactoryGirl.factory_by_name(factory_name)
|
7
|
+
attributes = convert_human_hash_to_attribute_hash({attribute => value.strip}, factory.associations)
|
8
|
+
attributes_find = {}
|
9
|
+
attributes.each do |k, v|
|
10
|
+
k = "#{k}_id" if v.is_a? ActiveRecord::Base
|
11
|
+
attributes_find[k] = v
|
12
|
+
end
|
13
|
+
model_class = factory.build_class
|
14
|
+
model_class.find(:first, :conditions => attributes_find) or
|
15
|
+
FactoryGirl.create(factory_name, attributes)
|
16
|
+
end
|
17
|
+
|
18
|
+
def convert_human_hash_to_attribute_hash(human_hash, associations = [])
|
19
|
+
human_hash.inject({}) do |attribute_hash, (human_key, value)|
|
20
|
+
key = human_key.downcase.gsub(' ', '_').to_sym
|
21
|
+
if association = associations.detect {|association| association.name == key }
|
22
|
+
value = convert_association_string_to_instance(association.factory, value)
|
23
|
+
end
|
24
|
+
attribute_hash.merge(key => value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
World(FactoryGirlStepHelpers)
|
30
|
+
|
31
|
+
FactoryGirl.factories.each do |factory|
|
32
|
+
Given /^the following (?:#{factory.human_name}|#{factory.human_name.pluralize}) exists?:$/ do |table|
|
33
|
+
table.hashes.each do |human_hash|
|
34
|
+
attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
|
35
|
+
factory.run(FactoryGirl::Proxy::Create, attributes)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
Given /^an? #{factory.human_name} exists$/ do
|
40
|
+
FactoryGirl.create(factory.name)
|
41
|
+
end
|
42
|
+
|
43
|
+
Given /^(\d+) #{factory.human_name.pluralize} exist$/ do |count|
|
44
|
+
count.to_i.times { FactoryGirl.create(factory.name) }
|
45
|
+
end
|
46
|
+
|
47
|
+
if factory.build_class.respond_to?(:columns)
|
48
|
+
factory.build_class.columns.each do |column|
|
49
|
+
human_column_name = column.name.downcase.gsub('_', ' ')
|
50
|
+
Given /^an? #{factory.human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
|
51
|
+
FactoryGirl.create(factory.name, column.name => value)
|
52
|
+
end
|
53
|
+
|
54
|
+
Given /^(\d+) #{factory.human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
|
55
|
+
count.to_i.times { FactoryGirl.create(factory.name, column.name => value) }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
# Extends ActiveRecord::Base to provide a make class method, which is an
|
5
|
+
# alternate syntax for defining factories.
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# require 'factory_girl/syntax/blueprint'
|
10
|
+
#
|
11
|
+
# User.blueprint do
|
12
|
+
# name { 'Billy Bob' }
|
13
|
+
# email { 'billy@bob.example.com' }
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# FactoryGirl.create(:user, :name => 'Johnny')
|
17
|
+
#
|
18
|
+
# This syntax was derived from Pete Yandell's machinist.
|
19
|
+
module Blueprint
|
20
|
+
module ActiveRecord #:nodoc:
|
21
|
+
|
22
|
+
def self.included(base) # :nodoc:
|
23
|
+
base.extend ClassMethods
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods #:nodoc:
|
27
|
+
|
28
|
+
def blueprint(&block)
|
29
|
+
instance = Factory.new(name.underscore, :class => self)
|
30
|
+
proxy = FactoryGirl::DefinitionProxy.new(instance)
|
31
|
+
proxy.instance_eval(&block)
|
32
|
+
FactoryGirl.register_factory(instance)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Blueprint::ActiveRecord)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
module Default
|
4
|
+
include Methods
|
5
|
+
|
6
|
+
def define(&block)
|
7
|
+
DSL.run(block)
|
8
|
+
end
|
9
|
+
|
10
|
+
class DSL
|
11
|
+
def self.run(block)
|
12
|
+
new.instance_eval(&block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def factory(name, options = {}, &block)
|
16
|
+
factory = Factory.new(name, options)
|
17
|
+
proxy = FactoryGirl::DefinitionProxy.new(factory)
|
18
|
+
proxy.instance_eval(&block)
|
19
|
+
if parent = options.delete(:parent)
|
20
|
+
factory.inherit_from(FactoryGirl.factory_by_name(parent))
|
21
|
+
end
|
22
|
+
FactoryGirl.register_factory(factory)
|
23
|
+
end
|
24
|
+
|
25
|
+
def sequence(name, start_value = 1, &block)
|
26
|
+
FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
extend Syntax::Default
|
33
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
# Extends ActiveRecord::Base to provide generation methods for factories.
|
5
|
+
#
|
6
|
+
# Usage:
|
7
|
+
#
|
8
|
+
# require 'factory_girl/syntax/generate'
|
9
|
+
#
|
10
|
+
# FactoryGirl.define do
|
11
|
+
# factory :user do
|
12
|
+
# name 'Billy Bob'
|
13
|
+
# email 'billy@bob.example.com'
|
14
|
+
# end
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# # Creates a saved instance without raising (same as saving the result
|
18
|
+
# # of FactoryGirl.build)
|
19
|
+
# User.generate(:name => 'Johnny')
|
20
|
+
#
|
21
|
+
# # Creates a saved instance and raises when invalid (same as
|
22
|
+
# # FactoryGirl.create)
|
23
|
+
# User.generate!
|
24
|
+
#
|
25
|
+
# # Creates an unsaved instance (same as FactoryGirl.build)
|
26
|
+
# User.spawn
|
27
|
+
#
|
28
|
+
# # Creates an instance and yields it to the passed block
|
29
|
+
# User.generate do |user|
|
30
|
+
# # ...do something with user...
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# This syntax was derived from Rick Bradley and Yossef Mendelssohn's
|
34
|
+
# object_daddy.
|
35
|
+
module Generate
|
36
|
+
module ActiveRecord #:nodoc:
|
37
|
+
|
38
|
+
def self.included(base) # :nodoc:
|
39
|
+
base.extend ClassMethods
|
40
|
+
end
|
41
|
+
|
42
|
+
module ClassMethods #:nodoc:
|
43
|
+
|
44
|
+
def generate(overrides = {}, &block)
|
45
|
+
factory = FactoryGirl.factory_by_name(name.underscore)
|
46
|
+
instance = factory.run(Proxy::Build, overrides)
|
47
|
+
instance.save
|
48
|
+
yield(instance) if block_given?
|
49
|
+
instance
|
50
|
+
end
|
51
|
+
|
52
|
+
def generate!(overrides = {}, &block)
|
53
|
+
factory = FactoryGirl.factory_by_name(name.underscore)
|
54
|
+
instance = factory.run(Proxy::Create, overrides)
|
55
|
+
yield(instance) if block_given?
|
56
|
+
instance
|
57
|
+
end
|
58
|
+
|
59
|
+
def spawn(overrides = {}, &block)
|
60
|
+
factory = FactoryGirl.factory_by_name(name.underscore)
|
61
|
+
instance = factory.run(Proxy::Build, overrides)
|
62
|
+
yield(instance) if block_given?
|
63
|
+
instance
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Generate::ActiveRecord)
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
# Extends ActiveRecord::Base to provide a make class method, which is a
|
5
|
+
# shortcut for FactoryGirl.create.
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# require 'factory_girl/syntax/make'
|
10
|
+
#
|
11
|
+
# FactoryGirl.define do
|
12
|
+
# factory :user do
|
13
|
+
# name 'Billy Bob'
|
14
|
+
# email 'billy@bob.example.com'
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# User.make(:name => 'Johnny')
|
19
|
+
#
|
20
|
+
# This syntax was derived from Pete Yandell's machinist.
|
21
|
+
module Make
|
22
|
+
module ActiveRecord #:nodoc:
|
23
|
+
|
24
|
+
def self.included(base) # :nodoc:
|
25
|
+
base.extend ClassMethods
|
26
|
+
end
|
27
|
+
|
28
|
+
module ClassMethods #:nodoc:
|
29
|
+
|
30
|
+
def make(overrides = {})
|
31
|
+
FactoryGirl.factory_by_name(name.underscore).run(Proxy::Create, overrides)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Make::ActiveRecord)
|
@@ -0,0 +1,86 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
module Methods
|
4
|
+
# Generates and returns a Hash of attributes from this factory. Attributes
|
5
|
+
# can be individually overridden by passing in a Hash of attribute => value
|
6
|
+
# pairs.
|
7
|
+
#
|
8
|
+
# Arguments:
|
9
|
+
# * name: +Symbol+ or +String+
|
10
|
+
# The name of the factory that should be used.
|
11
|
+
# * overrides: +Hash+
|
12
|
+
# Attributes to overwrite for this set.
|
13
|
+
#
|
14
|
+
# Returns: +Hash+
|
15
|
+
# A set of attributes that can be used to build an instance of the class
|
16
|
+
# this factory generates.
|
17
|
+
def attributes_for(name, overrides = {})
|
18
|
+
FactoryGirl.factory_by_name(name).run(Proxy::AttributesFor, overrides)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Generates and returns an instance from this factory. Attributes can be
|
22
|
+
# individually overridden by passing in a Hash of attribute => value pairs.
|
23
|
+
#
|
24
|
+
# Arguments:
|
25
|
+
# * name: +Symbol+ or +String+
|
26
|
+
# The name of the factory that should be used.
|
27
|
+
# * overrides: +Hash+
|
28
|
+
# Attributes to overwrite for this instance.
|
29
|
+
#
|
30
|
+
# Returns: +Object+
|
31
|
+
# An instance of the class this factory generates, with generated attributes
|
32
|
+
# assigned.
|
33
|
+
def build(name, overrides = {})
|
34
|
+
FactoryGirl.factory_by_name(name).run(Proxy::Build, overrides)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Generates, saves, and returns an instance from this factory. Attributes can
|
38
|
+
# be individually overridden by passing in a Hash of attribute => value
|
39
|
+
# pairs.
|
40
|
+
#
|
41
|
+
# Instances are saved using the +save!+ method, so ActiveRecord models will
|
42
|
+
# raise ActiveRecord::RecordInvalid exceptions for invalid attribute sets.
|
43
|
+
#
|
44
|
+
# Arguments:
|
45
|
+
# * name: +Symbol+ or +String+
|
46
|
+
# The name of the factory that should be used.
|
47
|
+
# * overrides: +Hash+
|
48
|
+
# Attributes to overwrite for this instance.
|
49
|
+
#
|
50
|
+
# Returns: +Object+
|
51
|
+
# A saved instance of the class this factory generates, with generated
|
52
|
+
# attributes assigned.
|
53
|
+
def create(name, overrides = {})
|
54
|
+
FactoryGirl.factory_by_name(name).run(Proxy::Create, overrides)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Generates and returns an object with all attributes from this factory
|
58
|
+
# stubbed out. Attributes can be individually overridden by passing in a Hash
|
59
|
+
# of attribute => value pairs.
|
60
|
+
#
|
61
|
+
# Arguments:
|
62
|
+
# * name: +Symbol+ or +String+
|
63
|
+
# The name of the factory that should be used.
|
64
|
+
# * overrides: +Hash+
|
65
|
+
# Attributes to overwrite for this instance.
|
66
|
+
#
|
67
|
+
# Returns: +Object+
|
68
|
+
# An object with generated attributes stubbed out.
|
69
|
+
def build_stubbed(name, overrides = {})
|
70
|
+
FactoryGirl.factory_by_name(name).run(Proxy::Stub, overrides)
|
71
|
+
end
|
72
|
+
|
73
|
+
# Generates and returns the next value in a sequence.
|
74
|
+
#
|
75
|
+
# Arguments:
|
76
|
+
# name: (Symbol)
|
77
|
+
# The name of the sequence that a value should be generated for.
|
78
|
+
#
|
79
|
+
# Returns:
|
80
|
+
# The next value in the sequence. (Object)
|
81
|
+
def generate(name)
|
82
|
+
FactoryGirl.sequence_by_name(name).next
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
# Adds a Sham module, which provides an alternate interface to
|
5
|
+
# FactoryGirl::Sequence.
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# require 'factory_girl/syntax/sham'
|
10
|
+
#
|
11
|
+
# Sham.email {|n| "somebody#{n}@example.com" }
|
12
|
+
#
|
13
|
+
# FactoryGirl.define do
|
14
|
+
# factory :user do
|
15
|
+
# email
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# Note that you can also use Faker, but it is recommended that you simply
|
20
|
+
# use a sequence as in the above example, as factory_girl does not provide
|
21
|
+
# protection against duplication in randomized sequences, and a randomized
|
22
|
+
# value does not provide any tangible benefits over an ascending sequence.
|
23
|
+
#
|
24
|
+
# This syntax was derived from Pete Yandell's machinist.
|
25
|
+
module Sham
|
26
|
+
module Sham #:nodoc:
|
27
|
+
def self.method_missing(name, *args, &block)
|
28
|
+
if block_given?
|
29
|
+
start_value = args.first
|
30
|
+
FactoryGirl.register_sequence(Sequence.new(name, start_value || 1, &block))
|
31
|
+
else
|
32
|
+
FactoryGirl.sequence_by_name(name).next
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# overrides name on Module
|
37
|
+
def self.name(&block)
|
38
|
+
method_missing('name', &block)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
include FactoryGirl::Syntax::Sham
|