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,52 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "modifying inherited factories with traits" do
4
- before do
5
- define_model('User', gender: :string, admin: :boolean, age: :integer)
6
- FactoryGirl.define do
7
- factory :user do
8
- trait(:female) { gender "Female" }
9
- trait(:male) { gender "Male" }
10
-
11
- trait(:young_admin) do
12
- admin true
13
- age 17
14
- end
15
-
16
- female
17
- young_admin
18
-
19
- factory :female_user do
20
- gender "Female"
21
- age 25
22
- end
23
-
24
- factory :male_user do
25
- gender "Male"
26
- end
27
- end
28
- end
29
- end
30
-
31
- it "returns the correct value for overridden attributes from traits" do
32
- expect(FactoryGirl.build(:male_user).gender).to eq "Male"
33
- end
34
-
35
- it "returns the correct value for overridden attributes from traits defining multiple attributes" do
36
- expect(FactoryGirl.build(:female_user).gender).to eq "Female"
37
- expect(FactoryGirl.build(:female_user).age).to eq 25
38
- expect(FactoryGirl.build(:female_user).admin).to eq true
39
- end
40
-
41
- it "allows modification of attributes created via traits" do
42
- FactoryGirl.modify do
43
- factory :male_user do
44
- age 20
45
- end
46
- end
47
-
48
- expect(FactoryGirl.build(:male_user).gender).to eq "Male"
49
- expect(FactoryGirl.build(:male_user).age).to eq 20
50
- expect(FactoryGirl.build(:male_user).admin).to eq true
51
- end
52
- end
@@ -1,32 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "association assignment from nested attributes" do
4
- before do
5
- define_model("Post", title: :string) do
6
- has_many :comments
7
- accepts_nested_attributes_for :comments
8
- end
9
-
10
- define_model("Comment", post_id: :integer, body: :text) do
11
- belongs_to :post
12
- end
13
-
14
- FactoryGirl.define do
15
- factory :post do
16
- comments_attributes { [FactoryGirl.attributes_for(:comment), FactoryGirl.attributes_for(:comment)] }
17
- end
18
-
19
- factory :comment do
20
- sequence(:body) { |n| "Body #{n}" }
21
- end
22
- end
23
- end
24
-
25
- it "assigns the correct amount of comments" do
26
- expect(FactoryGirl.create(:post).comments.count).to eq 2
27
- end
28
-
29
- it "assigns the correct amount of comments when overridden" do
30
- expect(FactoryGirl.create(:post, :comments_attributes => [FactoryGirl.attributes_for(:comment)]).comments.count).to eq 1
31
- end
32
- end
@@ -1,61 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "attribute overrides" do
4
- before do
5
- define_model('User', admin: :boolean)
6
- define_model('Post', title: :string,
7
- secure: :boolean,
8
- user_id: :integer) do
9
- belongs_to :user
10
-
11
- def secure=(value)
12
- return unless user && user.admin?
13
- write_attribute(:secure, value)
14
- end
15
- end
16
-
17
- FactoryGirl.define do
18
- factory :user do
19
- factory :admin do
20
- admin true
21
- end
22
- end
23
-
24
- factory :post do
25
- user
26
- title "default title"
27
- end
28
- end
29
- end
30
-
31
- let(:admin) { FactoryGirl.create(:admin) }
32
-
33
- let(:post_attributes) do
34
- { secure: false }
35
- end
36
-
37
- let(:non_admin_post_attributes) do
38
- post_attributes[:user] = FactoryGirl.create(:user)
39
- post_attributes
40
- end
41
-
42
- let(:admin_post_attributes) do
43
- post_attributes[:user] = admin
44
- post_attributes
45
- end
46
-
47
- context "with an admin posting" do
48
- subject { FactoryGirl.create(:post, admin_post_attributes) }
49
- its(:secure) { should eq false }
50
- end
51
-
52
- context "with a non-admin posting" do
53
- subject { FactoryGirl.create(:post, non_admin_post_attributes) }
54
- its(:secure) { should be_nil }
55
- end
56
-
57
- context "with no user posting" do
58
- subject { FactoryGirl.create(:post, post_attributes) }
59
- its(:secure) { should be_nil }
60
- end
61
- end
@@ -1,90 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "an instance generated by a factory that inherits from another factory" do
4
- before do
5
- define_model("User", name: :string, admin: :boolean, email: :string, upper_email: :string, login: :string)
6
-
7
- FactoryGirl.define do
8
- factory :user do
9
- name "John"
10
- email { "#{name.downcase}@example.com" }
11
- login { email }
12
-
13
- factory :admin do
14
- name "admin"
15
- admin true
16
- upper_email { email.upcase }
17
- end
18
-
19
- factory :guest do
20
- email { "#{name}-guest@example.com" }
21
- end
22
-
23
- factory :no_email do
24
- email ""
25
- end
26
-
27
- factory :bill do
28
- name { "Bill" } #block to make attribute dynamic
29
- end
30
- end
31
- end
32
- end
33
-
34
- describe "the parent class" do
35
- subject { FactoryGirl.create(:user) }
36
- it { should_not be_admin }
37
- its(:email) { should eq "john@example.com" }
38
- end
39
-
40
- describe "the child class redefining parent's static method used by a dynamic method" do
41
- subject { FactoryGirl.create(:admin) }
42
- it { should be_kind_of(User) }
43
- it { should be_admin }
44
- its(:name) { should eq "admin" }
45
- its(:email) { should eq "admin@example.com" }
46
- its(:upper_email) { should eq "ADMIN@EXAMPLE.COM"}
47
- end
48
-
49
- describe "the child class redefining parent's dynamic method" do
50
- subject { FactoryGirl.create(:guest) }
51
- it { should_not be_admin }
52
- its(:name) { should eq "John" }
53
- its(:email) { should eql "John-guest@example.com" }
54
- its(:login) { should eq "John-guest@example.com" }
55
- end
56
-
57
- describe "the child class redefining parent's dynamic attribute with static attribute" do
58
- subject { FactoryGirl.create(:no_email) }
59
- its(:email) { should eq "" }
60
- end
61
-
62
- describe "the child class redefining parent's static attribute with dynamic attribute" do
63
- subject { FactoryGirl.create(:bill) }
64
- its(:name) { should eq "Bill" }
65
- end
66
- end
67
-
68
- describe "nested factories with different parents" do
69
- before do
70
- define_model("User", name: :string)
71
-
72
- FactoryGirl.define do
73
- factory :user do
74
- name "Basic User"
75
-
76
- factory :male_user do
77
- name "John Doe"
78
- end
79
-
80
- factory :uppercase_male_user, parent: :male_user do
81
- after(:build) { |user| user.name = user.name.upcase }
82
- end
83
- end
84
- end
85
- end
86
-
87
- it "honors :parent over the factory block nesting" do
88
- expect(FactoryGirl.build(:uppercase_male_user).name).to eq "JOHN DOE"
89
- end
90
- end
@@ -1,20 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'setting private attributes' do
4
- it 'raises a NoMethodError' do
5
- define_class('User') do
6
- private
7
- attr_accessor :foo
8
- end
9
-
10
- FactoryGirl.define do
11
- factory :user do
12
- foo 123
13
- end
14
- end
15
-
16
- expect do
17
- FactoryGirl.build(:user)
18
- end.to raise_error NoMethodError, /foo=/
19
- end
20
- end
@@ -1,136 +0,0 @@
1
- require "spec_helper"
2
-
3
- shared_context "registering custom strategies" do
4
- before do
5
- define_class("NamedObject") do
6
- attr_accessor :name
7
- end
8
- end
9
-
10
- let(:custom_strategy) do
11
- Class.new do
12
- def result(evaluation)
13
- evaluation.object.tap do |instance|
14
- instance.name = "Custom strategy"
15
- end
16
- end
17
- end
18
- end
19
- end
20
-
21
- describe "register custom strategies" do
22
- include_context "registering custom strategies"
23
-
24
- before do
25
- FactoryGirl.define do
26
- factory :named_object do
27
- name "Great"
28
- end
29
- end
30
- end
31
-
32
- it "allows overriding default strategies" do
33
- expect(FactoryGirl.build(:named_object).name).to eq "Great"
34
- FactoryGirl.register_strategy(:build, custom_strategy)
35
- expect(FactoryGirl.build(:named_object).name).to eq "Custom strategy"
36
- end
37
-
38
- it "allows adding additional strategies" do
39
- FactoryGirl.register_strategy(:insert, custom_strategy)
40
-
41
- expect(FactoryGirl.build(:named_object).name).to eq "Great"
42
- expect(FactoryGirl.insert(:named_object).name).to eq "Custom strategy"
43
- end
44
-
45
- it "allows using the *_list method to build a list using a custom strategy" do
46
- FactoryGirl.register_strategy(:insert, custom_strategy)
47
-
48
- inserted_items = FactoryGirl.insert_list(:named_object, 2)
49
- expect(inserted_items.length).to eq 2
50
- expect(inserted_items.map(&:name)).to eq ["Custom strategy", "Custom strategy"]
51
- end
52
-
53
- it "allows using the *_pair method to build a list using a custom strategy" do
54
- FactoryGirl.register_strategy(:insert, custom_strategy)
55
-
56
- inserted_items = FactoryGirl.insert_pair(:named_object)
57
- expect(inserted_items.length).to eq 2
58
- expect(inserted_items.map(&:name)).to eq ["Custom strategy", "Custom strategy"]
59
- end
60
- end
61
-
62
- describe "including FactoryGirl::Syntax::Methods when custom strategies have been declared" do
63
- include FactoryGirl::Syntax::Methods
64
-
65
- include_context "registering custom strategies"
66
-
67
- before do
68
- FactoryGirl.define do
69
- factory :named_object do
70
- name "Great"
71
- end
72
- end
73
- end
74
-
75
- it "allows adding additional strategies" do
76
- FactoryGirl.register_strategy(:insert, custom_strategy)
77
-
78
- expect(insert(:named_object).name).to eq "Custom strategy"
79
- end
80
- end
81
-
82
- describe "associations without overriding :strategy" do
83
- include_context "registering custom strategies"
84
-
85
- before do
86
- define_model("Post", user_id: :integer) do
87
- belongs_to :user
88
- end
89
-
90
- define_model("User", name: :string)
91
-
92
- FactoryGirl.define do
93
- factory :post do
94
- user
95
- end
96
-
97
- factory :user do
98
- name "John Doe"
99
- end
100
- end
101
- end
102
-
103
- it "uses the overridden create strategy to create the association" do
104
- FactoryGirl.register_strategy(:create, custom_strategy)
105
- post = FactoryGirl.build(:post)
106
- expect(post.user.name).to eq "Custom strategy"
107
- end
108
- end
109
-
110
- describe "associations overriding :strategy" do
111
- include_context "registering custom strategies"
112
-
113
- before do
114
- define_model("Post", user_id: :integer) do
115
- belongs_to :user
116
- end
117
-
118
- define_model("User", name: :string)
119
-
120
- FactoryGirl.define do
121
- factory :post do
122
- association :user, strategy: :insert
123
- end
124
-
125
- factory :user do
126
- name "John Doe"
127
- end
128
- end
129
- end
130
-
131
- it "uses the overridden create strategy to create the association" do
132
- FactoryGirl.register_strategy(:insert, custom_strategy)
133
- post = FactoryGirl.build(:post)
134
- expect(post.user.name).to eq "Custom strategy"
135
- end
136
- end
@@ -1,52 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'sequences are evaluated in the correct context' do
4
- before do
5
- define_class("User") do
6
- attr_accessor :id
7
-
8
- def awesome
9
- 'aw yeah'
10
- end
11
- end
12
- end
13
-
14
- it 'builds a sequence calling sprintf correctly' do
15
- FactoryGirl.define do
16
- factory :sequence_with_sprintf, class: User do
17
- sequence(:id) { |n| sprintf("foo%d", n) }
18
- end
19
- end
20
-
21
- expect(FactoryGirl.build(:sequence_with_sprintf).id).to eq 'foo1'
22
- end
23
-
24
- it 'invokes the correct method on the instance' do
25
- FactoryGirl.define do
26
- factory :sequence_with_public_method, class: User do
27
- sequence(:id) { |n| public_method(:awesome).call }
28
- end
29
- end
30
-
31
- expect(FactoryGirl.build(:sequence_with_public_method).id).to eq 'aw yeah'
32
- end
33
-
34
- it 'invokes a method with no arguments on the instance' do
35
- FactoryGirl.define do
36
- factory :sequence_with_frozen, class: User do
37
- sequence(:id) { |n| frozen? }
38
- end
39
- end
40
-
41
- expect(FactoryGirl.build(:sequence_with_frozen).id).to be_false
42
- end
43
-
44
- it 'allows direct reference of a method in a sequence' do
45
- FactoryGirl.define do
46
- factory :sequence_referencing_attribute_directly, class: User do
47
- sequence(:id) { |n| "#{awesome}#{n}" }
48
- end
49
- end
50
- expect(FactoryGirl.build(:sequence_referencing_attribute_directly).id).to eq 'aw yeah1'
51
- end
52
- end