factory_girl 4.5.0 → 4.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +25 -5
  3. data/Appraisals +11 -7
  4. data/CONTRIBUTING.md +44 -6
  5. data/GETTING_STARTED.md +175 -59
  6. data/Gemfile +5 -0
  7. data/Gemfile.lock +86 -63
  8. data/LICENSE +1 -1
  9. data/NEWS +12 -1
  10. data/README.md +21 -10
  11. data/Rakefile +1 -3
  12. data/factory_girl.gemspec +6 -14
  13. data/gemfiles/3.2.gemfile +4 -1
  14. data/gemfiles/3.2.gemfile.lock +84 -52
  15. data/gemfiles/4.0.gemfile +4 -1
  16. data/gemfiles/4.0.gemfile.lock +75 -49
  17. data/gemfiles/4.1.gemfile +4 -1
  18. data/gemfiles/4.1.gemfile.lock +78 -48
  19. data/gemfiles/4.2.gemfile +10 -0
  20. data/gemfiles/4.2.gemfile.lock +123 -0
  21. data/gemfiles/5.0.gemfile +10 -0
  22. data/gemfiles/5.0.gemfile.lock +126 -0
  23. data/lib/factory_girl.rb +13 -4
  24. data/lib/factory_girl/attribute/dynamic.rb +4 -1
  25. data/lib/factory_girl/attribute_assigner.rb +2 -2
  26. data/lib/factory_girl/configuration.rb +4 -0
  27. data/lib/factory_girl/decorator.rb +2 -2
  28. data/lib/factory_girl/decorator/class_key_hash.rb +2 -1
  29. data/lib/factory_girl/definition_proxy.rb +2 -2
  30. data/lib/factory_girl/evaluator.rb +4 -0
  31. data/lib/factory_girl/factory_runner.rb +7 -1
  32. data/lib/factory_girl/find_definitions.rb +1 -1
  33. data/lib/factory_girl/linter.rb +65 -14
  34. data/lib/factory_girl/strategy/stub.rb +25 -1
  35. data/lib/factory_girl/strategy_syntax_method_registrar.rb +4 -0
  36. data/lib/factory_girl/version.rb +1 -1
  37. metadata +26 -185
  38. data/features/find_definitions.feature +0 -75
  39. data/features/step_definitions/database_steps.rb +0 -9
  40. data/features/step_definitions/factory_girl_steps.rb +0 -36
  41. data/features/support/env.rb +0 -10
  42. data/features/support/factories.rb +0 -16
  43. data/gemfiles/3.1.gemfile +0 -7
  44. data/gemfiles/3.1.gemfile.lock +0 -91
  45. data/spec/acceptance/activesupport_instrumentation_spec.rb +0 -62
  46. data/spec/acceptance/aliases_spec.rb +0 -19
  47. data/spec/acceptance/attribute_aliases_spec.rb +0 -45
  48. data/spec/acceptance/attribute_existing_on_object_spec.rb +0 -68
  49. data/spec/acceptance/attributes_for_spec.rb +0 -97
  50. data/spec/acceptance/attributes_from_instance_spec.rb +0 -53
  51. data/spec/acceptance/attributes_ordered_spec.rb +0 -51
  52. data/spec/acceptance/build_list_spec.rb +0 -56
  53. data/spec/acceptance/build_spec.rb +0 -86
  54. data/spec/acceptance/build_stubbed_spec.rb +0 -161
  55. data/spec/acceptance/callbacks_spec.rb +0 -253
  56. data/spec/acceptance/create_list_spec.rb +0 -97
  57. data/spec/acceptance/create_pair_spec.rb +0 -32
  58. data/spec/acceptance/create_spec.rb +0 -116
  59. data/spec/acceptance/define_child_before_parent_spec.rb +0 -21
  60. data/spec/acceptance/defining_methods_inside_a_factory_spec.rb +0 -17
  61. data/spec/acceptance/definition_camel_string_spec.rb +0 -15
  62. data/spec/acceptance/definition_spec.rb +0 -21
  63. data/spec/acceptance/definition_without_block_spec.rb +0 -15
  64. data/spec/acceptance/global_initialize_with_spec.rb +0 -82
  65. data/spec/acceptance/global_to_create_spec.rb +0 -122
  66. data/spec/acceptance/initialize_with_spec.rb +0 -217
  67. data/spec/acceptance/keyed_by_class_spec.rb +0 -22
  68. data/spec/acceptance/lint_spec.rb +0 -65
  69. data/spec/acceptance/modify_factories_spec.rb +0 -184
  70. data/spec/acceptance/modify_inherited_spec.rb +0 -52
  71. data/spec/acceptance/nested_attributes_spec.rb +0 -32
  72. data/spec/acceptance/overrides_spec.rb +0 -61
  73. data/spec/acceptance/parent_spec.rb +0 -90
  74. data/spec/acceptance/private_attributes_spec.rb +0 -20
  75. data/spec/acceptance/register_strategies_spec.rb +0 -136
  76. data/spec/acceptance/sequence_context_spec.rb +0 -52
  77. data/spec/acceptance/sequence_spec.rb +0 -61
  78. data/spec/acceptance/skip_create_spec.rb +0 -19
  79. data/spec/acceptance/stub_spec.rb +0 -62
  80. data/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb +0 -51
  81. data/spec/acceptance/traits_spec.rb +0 -727
  82. data/spec/acceptance/transient_attributes_spec.rb +0 -151
  83. data/spec/factory_girl/aliases_spec.rb +0 -31
  84. data/spec/factory_girl/attribute/association_spec.rb +0 -28
  85. data/spec/factory_girl/attribute/dynamic_spec.rb +0 -52
  86. data/spec/factory_girl/attribute/sequence_spec.rb +0 -16
  87. data/spec/factory_girl/attribute/static_spec.rb +0 -19
  88. data/spec/factory_girl/attribute_list_spec.rb +0 -143
  89. data/spec/factory_girl/attribute_spec.rb +0 -16
  90. data/spec/factory_girl/callback_spec.rb +0 -41
  91. data/spec/factory_girl/declaration/implicit_spec.rb +0 -25
  92. data/spec/factory_girl/declaration_list_spec.rb +0 -67
  93. data/spec/factory_girl/definition_proxy_spec.rb +0 -215
  94. data/spec/factory_girl/definition_spec.rb +0 -64
  95. data/spec/factory_girl/disallows_duplicates_registry_spec.rb +0 -19
  96. data/spec/factory_girl/evaluator_class_definer_spec.rb +0 -77
  97. data/spec/factory_girl/factory_spec.rb +0 -259
  98. data/spec/factory_girl/find_definitions_spec.rb +0 -110
  99. data/spec/factory_girl/null_factory_spec.rb +0 -13
  100. data/spec/factory_girl/null_object_spec.rb +0 -22
  101. data/spec/factory_girl/registry_spec.rb +0 -68
  102. data/spec/factory_girl/sequence_spec.rb +0 -95
  103. data/spec/factory_girl/strategy/attributes_for_spec.rb +0 -18
  104. data/spec/factory_girl/strategy/build_spec.rb +0 -7
  105. data/spec/factory_girl/strategy/create_spec.rb +0 -25
  106. data/spec/factory_girl/strategy/stub_spec.rb +0 -38
  107. data/spec/factory_girl/strategy_calculator_spec.rb +0 -29
  108. data/spec/factory_girl_spec.rb +0 -22
  109. data/spec/spec_helper.rb +0 -26
  110. data/spec/support/macros/define_constant.rb +0 -86
  111. data/spec/support/matchers/callback.rb +0 -9
  112. data/spec/support/matchers/declaration.rb +0 -83
  113. data/spec/support/matchers/delegate.rb +0 -44
  114. data/spec/support/matchers/trait.rb +0 -11
  115. data/spec/support/shared_examples/strategy.rb +0 -90
@@ -1,22 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe FactoryGirl::NullObject do
4
- let(:methods_to_respond_to) { %w[id age name admin?] }
5
- let(:methods_to_not_respond_to) { %w[email date_of_birth title] }
6
-
7
- subject { FactoryGirl::NullObject.new(methods_to_respond_to) }
8
-
9
- it "responds to the given methods" do
10
- methods_to_respond_to.each do |method_name|
11
- expect(subject.__send__(method_name)).to be_nil
12
- expect(subject).to respond_to(method_name)
13
- end
14
- end
15
-
16
- it "does not respond to other methods" do
17
- methods_to_not_respond_to.each do |method_name|
18
- expect { subject.__send__(method_name) }.to raise_error(NoMethodError)
19
- expect(subject).not_to respond_to(method_name)
20
- end
21
- end
22
- end
@@ -1,68 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FactoryGirl::Registry do
4
- let(:registered_object) { stub("registered object") }
5
- let(:second_registered_object) { stub("second registered object") }
6
-
7
- subject { FactoryGirl::Registry.new("Great thing") }
8
-
9
- it { should be_kind_of(Enumerable) }
10
-
11
- it "finds a registered object" do
12
- subject.register(:object_name, registered_object)
13
- expect(subject.find(:object_name)).to eq registered_object
14
- end
15
-
16
- it "finds a registered object with square brackets" do
17
- subject.register(:object_name, registered_object)
18
- expect(subject[:object_name]).to eq registered_object
19
- end
20
-
21
- it "raises when an object cannot be found" do
22
- expect { subject.find(:object_name) }.to raise_error(ArgumentError, "Great thing not registered: object_name")
23
- end
24
-
25
- it "adds and returns the object registered" do
26
- expect(subject.register(:object_name, registered_object)).to eq registered_object
27
- end
28
-
29
- it "knows that an object is registered by symbol" do
30
- subject.register(:object_name, registered_object)
31
- expect(subject).to be_registered(:object_name)
32
- end
33
-
34
- it "knows that an object is registered by string" do
35
- subject.register(:object_name, registered_object)
36
- expect(subject).to be_registered("object_name")
37
- end
38
-
39
- it "knows when an object is not registered" do
40
- expect(subject).not_to be_registered("bogus")
41
- end
42
-
43
- it "iterates registered objects" do
44
- subject.register(:first_object, registered_object)
45
- subject.register(:second_object, second_registered_object)
46
- expect(subject.to_a).to eq [registered_object, second_registered_object]
47
- end
48
-
49
- it "does not include duplicate objects with registered under different names" do
50
- subject.register(:first_object, registered_object)
51
- subject.register(:second_object, registered_object)
52
- expect(subject.to_a).to eq [registered_object]
53
- end
54
-
55
- it "clears registered factories" do
56
- subject.register(:object_name, registered_object)
57
- subject.clear
58
- expect(subject.count).to be_zero
59
- end
60
-
61
- it "registers classes" do
62
- define_class("User")
63
- subject.register(User, registered_object)
64
- expect(subject.to_a).to eq [registered_object]
65
- expect(subject.find(:user)).to eq registered_object
66
- expect(subject.find(User)).to eq registered_object
67
- end
68
- end
@@ -1,95 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FactoryGirl::Sequence do
4
- describe "a basic sequence" do
5
- let(:name) { :test }
6
- subject { FactoryGirl::Sequence.new(name) { |n| "=#{n}" } }
7
-
8
- its(:name) { should eq name }
9
- its(:names) { should eq [name] }
10
- its(:next) { should eq "=1" }
11
-
12
- describe "when incrementing" do
13
- before { subject.next }
14
- its(:next) { should eq "=2" }
15
- end
16
- end
17
-
18
- describe "a custom sequence" do
19
- subject { FactoryGirl::Sequence.new(:name, "A") { |n| "=#{n}" } }
20
- its(:next) { should eq "=A" }
21
-
22
- describe "when incrementing" do
23
- before { subject.next }
24
- its(:next) { should eq "=B" }
25
- end
26
- end
27
-
28
- describe "a sequence with aliases using default value" do
29
- subject { FactoryGirl::Sequence.new(:test, aliases: [:alias, :other]) { |n| "=#{n}" } }
30
- its(:next) { should eq "=1" }
31
- its(:names) { should eq [:test, :alias, :other] }
32
-
33
- describe "when incrementing" do
34
- before { subject.next }
35
- its(:next) { should eq "=2" }
36
- end
37
- end
38
-
39
- describe "a sequence with custom value and aliases" do
40
- subject { FactoryGirl::Sequence.new(:test, 3, aliases: [:alias, :other]) { |n| "=#{n}" } }
41
- its(:next) { should eq "=3" }
42
-
43
- describe "when incrementing" do
44
- before { subject.next }
45
- its(:next) { should eq "=4" }
46
- end
47
- end
48
-
49
- describe "a basic sequence without a block" do
50
- subject { FactoryGirl::Sequence.new(:name) }
51
- its(:next) { should eq 1 }
52
-
53
- describe "when incrementing" do
54
- before { subject.next }
55
- its(:next) { should eq 2 }
56
- end
57
- end
58
-
59
- describe "a custom sequence without a block" do
60
- subject { FactoryGirl::Sequence.new(:name, "A") }
61
- its(:next) { should eq "A" }
62
-
63
- describe "when incrementing" do
64
- before { subject.next }
65
- its(:next) { should eq "B" }
66
- end
67
- end
68
-
69
- describe "iterating over items in an enumerator" do
70
- subject { FactoryGirl::Sequence.new(:name, %w[foo bar].to_enum) { |n| "=#{n}" } }
71
-
72
- it "navigates to the next items until no items remain" do
73
- expect(subject.next).to eq "=foo"
74
- expect(subject.next).to eq "=bar"
75
- expect { subject.next }.to raise_error(StopIteration)
76
- end
77
- end
78
-
79
- describe "a custom sequence and scope" do
80
- subject { FactoryGirl::Sequence.new(:name, 'A') { |n| "=#{n}#{foo}" } }
81
- let(:scope) { stub('scope', foo: 'attribute') }
82
-
83
- it 'increments within the correct scope' do
84
- expect(subject.next(scope)).to eq '=Aattribute'
85
- end
86
-
87
- describe 'when incrementing' do
88
- before { subject.next(scope) }
89
-
90
- it 'increments within the correct scope' do
91
- expect(subject.next(scope)).to eq '=Battribute'
92
- end
93
- end
94
- end
95
- end
@@ -1,18 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FactoryGirl::Strategy::AttributesFor do
4
- let(:result) { { name: "John Doe", gender: "Male", admin: false } }
5
- let(:evaluation) { stub("evaluation", hash: result) }
6
-
7
- it_should_behave_like "strategy without association support"
8
-
9
- it "returns the hash from the evaluation" do
10
- expect(subject.result(evaluation)).to eq result
11
- end
12
-
13
- it "does not run the to_create block" do
14
- expect do
15
- subject.result(evaluation)
16
- end.to_not raise_error
17
- end
18
- end
@@ -1,7 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe FactoryGirl::Strategy::Build do
4
- it_should_behave_like "strategy with association support", :create
5
- it_should_behave_like "strategy with callbacks", :after_build
6
- it_should_behave_like "strategy with strategy: :build", :build
7
- end
@@ -1,25 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FactoryGirl::Strategy::Create do
4
- it_should_behave_like "strategy with association support", :create
5
- it_should_behave_like "strategy with callbacks", :after_build, :before_create, :after_create
6
-
7
- it "runs a custom create block" do
8
- evaluation_class = Class.new do
9
- def initialize
10
- @block_run = false
11
- end
12
-
13
- attr_reader :block_run
14
-
15
- def create(*instance)
16
- @block_run = true
17
- end
18
- end
19
-
20
- evaluation = evaluation_class.new
21
- evaluation.stubs(object: nil, notify: nil)
22
- subject.result(evaluation)
23
- expect(evaluation.block_run).to be_true
24
- end
25
- end
@@ -1,38 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FactoryGirl::Strategy::Stub do
4
- it_should_behave_like "strategy with association support", :build_stubbed
5
- it_should_behave_like "strategy with callbacks", :after_stub
6
- it_should_behave_like "strategy with strategy: :build", :build_stubbed
7
-
8
- context "asking for a result" do
9
- before { Timecop.freeze(Time.now) }
10
- let(:result_instance) do
11
- define_class("ResultInstance") do
12
- attr_accessor :id
13
- end.new
14
- end
15
-
16
- let(:evaluation) { stub("evaluation", object: result_instance, notify: true) }
17
-
18
- it { expect(subject.result(evaluation)).not_to be_new_record }
19
- it { expect(subject.result(evaluation)).to be_persisted }
20
-
21
- it "assigns created_at" do
22
- created_at = subject.result(evaluation).created_at
23
- expect(created_at).to eq Time.now
24
-
25
- Timecop.travel(150000)
26
-
27
- expect(subject.result(evaluation).created_at).to eq created_at
28
- end
29
-
30
- [:save, :destroy, :connection, :reload, :update_attribute, :update_column].each do |database_method|
31
- it "raises when attempting to connect to the database by calling #{database_method}" do
32
- expect do
33
- subject.result(evaluation).send(database_method)
34
- end.to raise_error(RuntimeError, "stubbed models are not allowed to access the database - #{subject.result(evaluation).class}##{database_method}()")
35
- end
36
- end
37
- end
38
- end
@@ -1,29 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe FactoryGirl::StrategyCalculator do
4
- let(:strategy) do
5
- define_class("MyAwesomeClass")
6
- end
7
-
8
- context "when a class" do
9
- subject { FactoryGirl::StrategyCalculator.new(strategy).strategy }
10
-
11
- it "returns the class passed" do
12
- expect(subject).to eq strategy
13
- end
14
- end
15
-
16
- context "when a symbol" do
17
- before { FactoryGirl.stubs(:strategy_by_name).returns(strategy) }
18
- subject { FactoryGirl::StrategyCalculator.new(:build).strategy }
19
-
20
- it "finds the strategy by name" do
21
- subject
22
- expect(FactoryGirl).to have_received(:strategy_by_name).with(:build)
23
- end
24
-
25
- it "returns the strategy found" do
26
- expect(subject).to eq strategy
27
- end
28
- end
29
- end
@@ -1,22 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe FactoryGirl do
4
- let(:factory) { FactoryGirl::Factory.new(:object) }
5
- let(:sequence) { FactoryGirl::Sequence.new(:email) }
6
- let(:trait) { FactoryGirl::Trait.new(:admin) }
7
-
8
- it "finds a registered factory" do
9
- FactoryGirl.register_factory(factory)
10
- expect(FactoryGirl.factory_by_name(factory.name)).to eq factory
11
- end
12
-
13
- it "finds a registered sequence" do
14
- FactoryGirl.register_sequence(sequence)
15
- expect(FactoryGirl.sequence_by_name(sequence.name)).to eq sequence
16
- end
17
-
18
- it "finds a registered trait" do
19
- FactoryGirl.register_trait(trait)
20
- expect(FactoryGirl.trait_by_name(trait.name)).to eq trait
21
- end
22
- end
@@ -1,26 +0,0 @@
1
- $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
- $LOAD_PATH << File.join(File.dirname(__FILE__))
3
-
4
- require 'rubygems'
5
- require 'rspec'
6
- require 'rspec/autorun'
7
-
8
- require "simplecov"
9
-
10
- require 'factory_girl'
11
- require "mocha/api"
12
- require "bourne"
13
- require "timecop"
14
-
15
- Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) }
16
-
17
- RSpec.configure do |config|
18
- config.mock_framework = :mocha
19
-
20
- config.include DeclarationMatchers
21
-
22
- config.after do
23
- Timecop.return
24
- FactoryGirl.reload
25
- end
26
- end
@@ -1,86 +0,0 @@
1
- require 'active_record'
2
-
3
- module DefineConstantMacros
4
- def define_class(path, base = Object, &block)
5
- namespace, class_name = *constant_path(path)
6
- klass = Class.new(base)
7
- namespace.const_set(class_name, klass)
8
- klass.class_eval(&block) if block_given?
9
- @defined_constants << path
10
- klass
11
- end
12
-
13
- def define_model(name, columns = {}, &block)
14
- model = define_class(name, ActiveRecord::Base, &block)
15
- create_table(model.table_name) do |table|
16
- columns.each do |column_name, type|
17
- table.column column_name, type
18
- end
19
- end
20
- model
21
- end
22
-
23
- def create_table(table_name, &block)
24
- connection = ActiveRecord::Base.connection
25
-
26
- begin
27
- connection.execute("DROP TABLE IF EXISTS #{table_name}")
28
- connection.create_table(table_name, &block)
29
- @created_tables << table_name
30
- connection
31
- rescue Exception => exception
32
- connection.execute("DROP TABLE IF EXISTS #{table_name}")
33
- raise exception
34
- end
35
- end
36
-
37
- def constant_path(constant_name)
38
- names = constant_name.split('::')
39
- class_name = names.pop
40
- namespace = names.inject(Object) { |result, name| result.const_get(name) }
41
- [namespace, class_name]
42
- end
43
-
44
- def default_constants
45
- @defined_constants ||= []
46
- @created_tables ||= []
47
- end
48
-
49
- def clear_generated_constants
50
- @defined_constants.reverse.each do |path|
51
- namespace, class_name = *constant_path(path)
52
- namespace.send(:remove_const, class_name)
53
- end
54
-
55
- @defined_constants.clear
56
- end
57
-
58
- def clear_generated_tables
59
- @created_tables.each do |table_name|
60
- ActiveRecord::Base.
61
- connection.
62
- execute("DROP TABLE IF EXISTS #{table_name}")
63
- end
64
- @created_tables.clear
65
- end
66
- end
67
-
68
- RSpec.configure do |config|
69
- config.include DefineConstantMacros
70
-
71
- config.before(:all) do
72
- ActiveRecord::Base.establish_connection(
73
- adapter: 'sqlite3',
74
- database: File.join(File.dirname(__FILE__), 'test.db')
75
- )
76
- end
77
-
78
- config.before do
79
- default_constants
80
- end
81
-
82
- config.after do
83
- clear_generated_constants
84
- clear_generated_tables
85
- end
86
- end