ffactory_girl 4.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +9 -0
  3. data/.gitignore +9 -0
  4. data/.rspec +2 -0
  5. data/.simplecov +4 -0
  6. data/.travis.yml +38 -0
  7. data/.yardopts +5 -0
  8. data/Appraisals +19 -0
  9. data/CONTRIBUTING.md +60 -0
  10. data/GETTING_STARTED.md +1391 -0
  11. data/Gemfile +8 -0
  12. data/Gemfile.lock +116 -0
  13. data/LICENSE +19 -0
  14. data/NAME.md +11 -0
  15. data/NEWS +290 -0
  16. data/README.md +98 -0
  17. data/Rakefile +36 -0
  18. data/cucumber.yml +1 -0
  19. data/factory_girl.gemspec +36 -0
  20. data/gemfiles/3.2.gemfile +10 -0
  21. data/gemfiles/3.2.gemfile.lock +105 -0
  22. data/gemfiles/4.0.gemfile +10 -0
  23. data/gemfiles/4.0.gemfile.lock +105 -0
  24. data/gemfiles/4.1.gemfile +10 -0
  25. data/gemfiles/4.1.gemfile.lock +104 -0
  26. data/gemfiles/4.2.gemfile +10 -0
  27. data/gemfiles/4.2.gemfile.lock +104 -0
  28. data/gemfiles/5.0.gemfile +10 -0
  29. data/gemfiles/5.0.gemfile.lock +103 -0
  30. data/lib/factory_girl/aliases.rb +18 -0
  31. data/lib/factory_girl/attribute/association.rb +27 -0
  32. data/lib/factory_girl/attribute/dynamic.rb +24 -0
  33. data/lib/factory_girl/attribute/sequence.rb +16 -0
  34. data/lib/factory_girl/attribute/static.rb +16 -0
  35. data/lib/factory_girl/attribute.rb +62 -0
  36. data/lib/factory_girl/attribute_assigner.rb +97 -0
  37. data/lib/factory_girl/attribute_list.rb +67 -0
  38. data/lib/factory_girl/callback.rb +40 -0
  39. data/lib/factory_girl/callbacks_observer.rb +21 -0
  40. data/lib/factory_girl/configuration.rb +37 -0
  41. data/lib/factory_girl/declaration/association.rb +28 -0
  42. data/lib/factory_girl/declaration/dynamic.rb +26 -0
  43. data/lib/factory_girl/declaration/implicit.rb +33 -0
  44. data/lib/factory_girl/declaration/static.rb +26 -0
  45. data/lib/factory_girl/declaration.rb +23 -0
  46. data/lib/factory_girl/declaration_list.rb +49 -0
  47. data/lib/factory_girl/decorator/attribute_hash.rb +16 -0
  48. data/lib/factory_girl/decorator/class_key_hash.rb +28 -0
  49. data/lib/factory_girl/decorator/disallows_duplicates_registry.rb +13 -0
  50. data/lib/factory_girl/decorator/invocation_tracker.rb +19 -0
  51. data/lib/factory_girl/decorator/new_constructor.rb +12 -0
  52. data/lib/factory_girl/decorator.rb +21 -0
  53. data/lib/factory_girl/definition.rb +136 -0
  54. data/lib/factory_girl/definition_hierarchy.rb +48 -0
  55. data/lib/factory_girl/definition_proxy.rb +174 -0
  56. data/lib/factory_girl/errors.rb +25 -0
  57. data/lib/factory_girl/evaluation.rb +27 -0
  58. data/lib/factory_girl/evaluator.rb +82 -0
  59. data/lib/factory_girl/evaluator_class_definer.rb +20 -0
  60. data/lib/factory_girl/factory.rb +163 -0
  61. data/lib/factory_girl/factory_runner.rb +33 -0
  62. data/lib/factory_girl/find_definitions.rb +25 -0
  63. data/lib/factory_girl/linter.rb +97 -0
  64. data/lib/factory_girl/null_factory.rb +18 -0
  65. data/lib/factory_girl/null_object.rb +24 -0
  66. data/lib/factory_girl/registry.rb +38 -0
  67. data/lib/factory_girl/reload.rb +8 -0
  68. data/lib/factory_girl/sequence.rb +62 -0
  69. data/lib/factory_girl/strategy/attributes_for.rb +13 -0
  70. data/lib/factory_girl/strategy/build.rb +15 -0
  71. data/lib/factory_girl/strategy/create.rb +18 -0
  72. data/lib/factory_girl/strategy/null.rb +11 -0
  73. data/lib/factory_girl/strategy/stub.rb +111 -0
  74. data/lib/factory_girl/strategy_calculator.rb +26 -0
  75. data/lib/factory_girl/strategy_syntax_method_registrar.rb +54 -0
  76. data/lib/factory_girl/syntax/default.rb +76 -0
  77. data/lib/factory_girl/syntax/methods.rb +111 -0
  78. data/lib/factory_girl/syntax.rb +7 -0
  79. data/lib/factory_girl/syntax_runner.rb +6 -0
  80. data/lib/factory_girl/trait.rb +30 -0
  81. data/lib/factory_girl/version.rb +3 -0
  82. data/lib/factory_girl.rb +156 -0
  83. metadata +271 -0
@@ -0,0 +1,25 @@
1
+ module FactoryGirl
2
+ class << self
3
+ # An Array of strings specifying locations that should be searched for
4
+ # factory definitions. By default, factory_girl will attempt to require
5
+ # "factories", "test/factories" and "spec/factories". Only the first
6
+ # existing file will be loaded.
7
+ attr_accessor :definition_file_paths
8
+ end
9
+
10
+ self.definition_file_paths = %w(factories test/factories spec/factories)
11
+
12
+ def self.find_definitions
13
+ absolute_definition_file_paths = definition_file_paths.map { |path| File.expand_path(path) }
14
+
15
+ absolute_definition_file_paths.uniq.each do |path|
16
+ load("#{path}.rb") if File.exist?("#{path}.rb")
17
+
18
+ if File.directory? path
19
+ Dir[File.join(path, '**', '*.rb')].sort.each do |file|
20
+ load file
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,97 @@
1
+ module FactoryGirl
2
+ class Linter
3
+
4
+ def initialize(factories, linting_strategy, factory_strategy = :create)
5
+ @factories_to_lint = factories
6
+ @linting_method = "lint_#{linting_strategy}"
7
+ @factory_strategy = factory_strategy
8
+ @invalid_factories = calculate_invalid_factories
9
+ end
10
+
11
+ def lint!
12
+ if invalid_factories.any?
13
+ raise InvalidFactoryError, error_message
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :factories_to_lint, :invalid_factories, :factory_strategy
20
+
21
+ def calculate_invalid_factories
22
+ factories_to_lint.reduce(Hash.new([])) do |result, factory|
23
+ errors = send(@linting_method, factory)
24
+ result[factory] |= errors unless errors.empty?
25
+ result
26
+ end
27
+ end
28
+
29
+ class FactoryError
30
+ def initialize(wrapped_error, factory)
31
+ @wrapped_error = wrapped_error
32
+ @factory = factory
33
+ end
34
+
35
+ def message
36
+ message = @wrapped_error.message
37
+ "* #{location} - #{message} (#{@wrapped_error.class.name})"
38
+ end
39
+
40
+ def location
41
+ @factory.name
42
+ end
43
+ end
44
+
45
+ class FactoryTraitError < FactoryError
46
+ def initialize(wrapped_error, factory, trait_name)
47
+ super(wrapped_error, factory)
48
+ @trait_name = trait_name
49
+ end
50
+
51
+ def location
52
+ "#{@factory.name}+#{@trait_name}"
53
+ end
54
+ end
55
+
56
+ def lint_factory(factory)
57
+ result = []
58
+ begin
59
+ FactoryGirl.public_send(factory_strategy, factory.name)
60
+ rescue => error
61
+ result |= [FactoryError.new(error, factory)]
62
+ end
63
+ result
64
+ end
65
+
66
+ def lint_traits(factory)
67
+ result = []
68
+ factory.definition.defined_traits.map(&:name).each do |trait_name|
69
+ begin
70
+ FactoryGirl.public_send(factory_strategy, factory.name, trait_name)
71
+ rescue => error
72
+ result |=
73
+ [FactoryTraitError.new(error, factory, trait_name)]
74
+ end
75
+ end
76
+ result
77
+ end
78
+
79
+ def lint_factory_and_traits(factory)
80
+ errors = lint_factory(factory)
81
+ errors |= lint_traits(factory)
82
+ errors
83
+ end
84
+
85
+ def error_message
86
+ lines = invalid_factories.map do |_factory, exceptions|
87
+ exceptions.map(&:message)
88
+ end.flatten
89
+
90
+ <<-ERROR_MESSAGE.strip
91
+ The following factories are invalid:
92
+
93
+ #{lines.join("\n")}
94
+ ERROR_MESSAGE
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,18 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class NullFactory
4
+ attr_reader :definition
5
+
6
+ def initialize
7
+ @definition = Definition.new(:null_factory)
8
+ end
9
+
10
+ delegate :defined_traits, :callbacks, :attributes, :constructor,
11
+ :to_create, to: :definition
12
+
13
+ def compile; end
14
+ def class_name; end
15
+ def evaluator_class; FactoryGirl::Evaluator; end
16
+ def hierarchy_class; FactoryGirl::DefinitionHierarchy; end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class NullObject < ::BasicObject
4
+ def initialize(methods_to_respond_to)
5
+ @methods_to_respond_to = methods_to_respond_to.map(&:to_s)
6
+ end
7
+
8
+ def method_missing(name, *args, &block)
9
+ if respond_to?(name)
10
+ nil
11
+ else
12
+ super
13
+ end
14
+ end
15
+
16
+ def respond_to?(method, include_private=false)
17
+ @methods_to_respond_to.include? method.to_s
18
+ end
19
+
20
+ def respond_to_missing?(*args)
21
+ false
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ module FactoryGirl
2
+ class Registry
3
+ include Enumerable
4
+
5
+ attr_reader :name
6
+
7
+ def initialize(name)
8
+ @name = name
9
+ @items = Decorator::ClassKeyHash.new({})
10
+ end
11
+
12
+ def clear
13
+ @items.clear
14
+ end
15
+
16
+ def each(&block)
17
+ @items.values.uniq.each(&block)
18
+ end
19
+
20
+ def find(name)
21
+ if registered?(name)
22
+ @items[name]
23
+ else
24
+ raise ArgumentError, "#{@name} not registered: #{name}"
25
+ end
26
+ end
27
+
28
+ alias :[] :find
29
+
30
+ def register(name, item)
31
+ @items[name] = item
32
+ end
33
+
34
+ def registered?(name)
35
+ @items.key?(name)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,8 @@
1
+ module FactoryGirl
2
+ def self.reload
3
+ reset_configuration
4
+ register_default_strategies
5
+ register_default_callbacks
6
+ find_definitions
7
+ end
8
+ end
@@ -0,0 +1,62 @@
1
+ module FactoryGirl
2
+
3
+ # Sequences are defined using sequence within a FactoryGirl.define block.
4
+ # Sequence values are generated using next.
5
+ # @api private
6
+ class Sequence
7
+ attr_reader :name
8
+
9
+ def initialize(name, *args, &proc)
10
+ @name = name
11
+ @proc = proc
12
+
13
+ options = args.extract_options!
14
+ @value = args.first || 1
15
+ @aliases = options.fetch(:aliases) { [] }
16
+
17
+ if !@value.respond_to?(:peek)
18
+ @value = EnumeratorAdapter.new(@value)
19
+ end
20
+ end
21
+
22
+ def next(scope = nil)
23
+ if @proc && scope
24
+ scope.instance_exec(value, &@proc)
25
+ elsif @proc
26
+ @proc.call(value)
27
+ else
28
+ value
29
+ end
30
+ ensure
31
+ increment_value
32
+ end
33
+
34
+ def names
35
+ [@name] + @aliases
36
+ end
37
+
38
+ private
39
+
40
+ def value
41
+ @value.peek
42
+ end
43
+
44
+ def increment_value
45
+ @value.next
46
+ end
47
+
48
+ class EnumeratorAdapter
49
+ def initialize(value)
50
+ @value = value
51
+ end
52
+
53
+ def peek
54
+ @value
55
+ end
56
+
57
+ def next
58
+ @value = @value.next
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,13 @@
1
+ module FactoryGirl
2
+ module Strategy
3
+ class AttributesFor
4
+ def association(runner)
5
+ runner.run(:null)
6
+ end
7
+
8
+ def result(evaluation)
9
+ evaluation.hash
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module FactoryGirl
2
+ module Strategy
3
+ class Build
4
+ def association(runner)
5
+ runner.run
6
+ end
7
+
8
+ def result(evaluation)
9
+ evaluation.object.tap do |instance|
10
+ evaluation.notify(:after_build, instance)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module FactoryGirl
2
+ module Strategy
3
+ class Create
4
+ def association(runner)
5
+ runner.run
6
+ end
7
+
8
+ def result(evaluation)
9
+ evaluation.object.tap do |instance|
10
+ evaluation.notify(:after_build, instance)
11
+ evaluation.notify(:before_create, instance)
12
+ evaluation.create(instance)
13
+ evaluation.notify(:after_create, instance)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ module FactoryGirl
2
+ module Strategy
3
+ class Null
4
+ def association(runner)
5
+ end
6
+
7
+ def result(evaluation)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,111 @@
1
+ module FactoryGirl
2
+ module Strategy
3
+ class Stub
4
+ @@next_id = 1000
5
+
6
+ DISABLED_PERSISTENCE_METHODS = [
7
+ :connection,
8
+ :decrement!,
9
+ :decrement,
10
+ :delete,
11
+ :destroy!,
12
+ :destroy,
13
+ :increment!,
14
+ :increment,
15
+ :reload,
16
+ :save!,
17
+ :save,
18
+ :toggle!,
19
+ :toggle,
20
+ :touch,
21
+ :update!,
22
+ :update,
23
+ :update_attribute,
24
+ :update_attributes!,
25
+ :update_attributes,
26
+ :update_column,
27
+ :update_columns,
28
+ ].freeze
29
+
30
+ def association(runner)
31
+ runner.run(:build_stubbed)
32
+ end
33
+
34
+ def result(evaluation)
35
+ evaluation.object.tap do |instance|
36
+ stub_database_interaction_on_result(instance)
37
+ clear_changed_attributes_on_result(instance)
38
+ evaluation.notify(:after_stub, instance)
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def next_id
45
+ @@next_id += 1
46
+ end
47
+
48
+ def stub_database_interaction_on_result(result_instance)
49
+ result_instance.id ||= next_id
50
+
51
+ result_instance.instance_eval do
52
+ def persisted?
53
+ !new_record?
54
+ end
55
+
56
+ def new_record?
57
+ id.nil?
58
+ end
59
+
60
+ def destroyed?
61
+ nil
62
+ end
63
+
64
+ DISABLED_PERSISTENCE_METHODS.each do |write_method|
65
+ define_singleton_method(write_method) do |*args|
66
+ raise "stubbed models are not allowed to access the database - #{self.class}##{write_method}(#{args.join(",")})"
67
+ end
68
+ end
69
+ end
70
+
71
+ created_at_missing_default = result_instance.respond_to?(:created_at) && !result_instance.created_at
72
+ result_instance_missing_created_at = !result_instance.respond_to?(:created_at)
73
+
74
+ if created_at_missing_default || result_instance_missing_created_at
75
+ result_instance.instance_eval do
76
+ def created_at
77
+ @created_at ||= Time.now.in_time_zone
78
+ end
79
+ end
80
+ end
81
+
82
+ has_updated_at = result_instance.respond_to?(:updated_at)
83
+ updated_at_no_default = has_updated_at && !result_instance.updated_at
84
+ result_instance_missing_updated_at = !has_updated_at
85
+
86
+ if updated_at_no_default || result_instance_missing_updated_at
87
+ result_instance.instance_eval do
88
+ def updated_at
89
+ @updated_at ||= Time.current
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ def clear_changed_attributes_on_result(result_instance)
96
+ unless result_instance.respond_to?(:clear_changes_information)
97
+ result_instance.extend ActiveModelDirtyBackport
98
+ end
99
+
100
+ result_instance.clear_changes_information
101
+ end
102
+ end
103
+
104
+ module ActiveModelDirtyBackport
105
+ def clear_changes_information
106
+ @previously_changed = ActiveSupport::HashWithIndifferentAccess.new
107
+ @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,26 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class StrategyCalculator
4
+ def initialize(name_or_object)
5
+ @name_or_object = name_or_object
6
+ end
7
+
8
+ def strategy
9
+ if strategy_is_object?
10
+ @name_or_object
11
+ else
12
+ strategy_name_to_object
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def strategy_is_object?
19
+ @name_or_object.is_a?(Class)
20
+ end
21
+
22
+ def strategy_name_to_object
23
+ FactoryGirl.strategy_by_name(@name_or_object)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,54 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class StrategySyntaxMethodRegistrar
4
+ def initialize(strategy_name)
5
+ @strategy_name = strategy_name
6
+ end
7
+
8
+ def define_strategy_methods
9
+ define_singular_strategy_method
10
+ define_list_strategy_method
11
+ define_pair_strategy_method
12
+ end
13
+
14
+ private
15
+
16
+ def define_singular_strategy_method
17
+ strategy_name = @strategy_name
18
+
19
+ define_syntax_method(strategy_name) do |name, *traits_and_overrides, &block|
20
+ FactoryRunner.new(name, strategy_name, traits_and_overrides).run(&block)
21
+ end
22
+ end
23
+
24
+ def define_list_strategy_method
25
+ strategy_name = @strategy_name
26
+
27
+ define_syntax_method("#{strategy_name}_list") do |name, amount, *traits_and_overrides, &block|
28
+ unless amount.respond_to?(:times)
29
+ raise ArgumentError, "count missing for #{strategy_name}_list"
30
+ end
31
+
32
+ amount.times.map { send(strategy_name, name, *traits_and_overrides, &block) }
33
+ end
34
+ end
35
+
36
+ def define_pair_strategy_method
37
+ strategy_name = @strategy_name
38
+
39
+ define_syntax_method("#{strategy_name}_pair") do |name, *traits_and_overrides, &block|
40
+ 2.times.map { send(strategy_name, name, *traits_and_overrides, &block) }
41
+ end
42
+ end
43
+
44
+ def define_syntax_method(name, &block)
45
+ FactoryGirl::Syntax::Methods.module_exec do
46
+ if method_defined?(name) || private_method_defined?(name)
47
+ undef_method(name)
48
+ end
49
+
50
+ define_method(name, &block)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,76 @@
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
+ def modify(&block)
11
+ ModifyDSL.run(block)
12
+ end
13
+
14
+ class DSL
15
+ def factory(name, options = {}, &block)
16
+ factory = Factory.new(name, options)
17
+ proxy = FactoryGirl::DefinitionProxy.new(factory.definition)
18
+ proxy.instance_eval(&block) if block_given?
19
+
20
+ FactoryGirl.register_factory(factory)
21
+
22
+ proxy.child_factories.each do |(child_name, child_options, child_block)|
23
+ parent_factory = child_options.delete(:parent) || name
24
+ factory(child_name, child_options.merge(parent: parent_factory), &child_block)
25
+ end
26
+ end
27
+
28
+ def sequence(name, *args, &block)
29
+ FactoryGirl.register_sequence(Sequence.new(name, *args, &block))
30
+ end
31
+
32
+ def trait(name, &block)
33
+ FactoryGirl.register_trait(Trait.new(name, &block))
34
+ end
35
+
36
+ def to_create(&block)
37
+ FactoryGirl.to_create(&block)
38
+ end
39
+
40
+ def skip_create
41
+ FactoryGirl.skip_create
42
+ end
43
+
44
+ def initialize_with(&block)
45
+ FactoryGirl.initialize_with(&block)
46
+ end
47
+
48
+ def self.run(block)
49
+ new.instance_eval(&block)
50
+ end
51
+
52
+ delegate :before, :after, :callback, to: :configuration
53
+
54
+ private
55
+
56
+ def configuration
57
+ FactoryGirl.configuration
58
+ end
59
+ end
60
+
61
+ class ModifyDSL
62
+ def factory(name, options = {}, &block)
63
+ factory = FactoryGirl.factory_by_name(name)
64
+ proxy = FactoryGirl::DefinitionProxy.new(factory.definition.overridable)
65
+ proxy.instance_eval(&block)
66
+ end
67
+
68
+ def self.run(block)
69
+ new.instance_eval(&block)
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ extend Syntax::Default
76
+ end