challah 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. data/CHANGELOG.md +9 -2
  2. data/README.md +18 -18
  3. data/lib/challah/test.rb +22 -5
  4. data/lib/challah/version.rb +1 -1
  5. data/test/audit_test.rb +32 -32
  6. data/test/controller_test.rb +9 -9
  7. data/test/cookie_store_test.rb +25 -25
  8. data/test/factories.rb +7 -7
  9. data/test/helper.rb +14 -12
  10. data/test/permission_test.rb +14 -14
  11. data/test/restrictions_controller_test.rb +29 -30
  12. data/test/role_test.rb +26 -26
  13. data/test/session_test.rb +65 -65
  14. data/test/sessions_controller_test.rb +13 -13
  15. data/test/simple_cookie_store_test.rb +25 -25
  16. data/test/user_test.rb +79 -79
  17. data/vendor/bundle/cache/factory_girl-3.0.0.gem +0 -0
  18. data/vendor/bundle/gems/factory_girl-3.0.0/Appraisals +11 -0
  19. data/vendor/bundle/gems/factory_girl-3.0.0/CONTRIBUTION_GUIDELINES.md +10 -0
  20. data/vendor/bundle/gems/factory_girl-3.0.0/Changelog +168 -0
  21. data/vendor/bundle/gems/factory_girl-3.0.0/GETTING_STARTED.md +735 -0
  22. data/vendor/bundle/gems/factory_girl-3.0.0/Gemfile +5 -0
  23. data/vendor/bundle/gems/factory_girl-3.0.0/Gemfile.lock +95 -0
  24. data/vendor/bundle/gems/factory_girl-3.0.0/LICENSE +19 -0
  25. data/vendor/bundle/gems/factory_girl-3.0.0/README.md +70 -0
  26. data/vendor/bundle/gems/factory_girl-3.0.0/Rakefile +38 -0
  27. data/vendor/bundle/gems/factory_girl-3.0.0/cucumber.yml +1 -0
  28. data/vendor/bundle/gems/factory_girl-3.0.0/factory_girl.gemspec +38 -0
  29. data/vendor/bundle/gems/factory_girl-3.0.0/features/factory_girl_steps.feature +237 -0
  30. data/vendor/bundle/gems/factory_girl-3.0.0/features/find_definitions.feature +75 -0
  31. data/vendor/bundle/gems/factory_girl-3.0.0/features/step_definitions/database_steps.rb +42 -0
  32. data/vendor/bundle/gems/factory_girl-3.0.0/features/step_definitions/factory_girl_steps.rb +43 -0
  33. data/vendor/bundle/gems/factory_girl-3.0.0/features/support/env.rb +11 -0
  34. data/vendor/bundle/gems/factory_girl-3.0.0/features/support/factories.rb +119 -0
  35. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/2.3.gemfile +7 -0
  36. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.0.gemfile +7 -0
  37. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.0.gemfile.lock +86 -0
  38. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.1.gemfile +7 -0
  39. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.1.gemfile.lock +95 -0
  40. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.2.gemfile +7 -0
  41. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.2.gemfile.lock +93 -0
  42. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl.rb +69 -0
  43. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/aliases.rb +19 -0
  44. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute.rb +40 -0
  45. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/association.rb +23 -0
  46. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/dynamic.rb +20 -0
  47. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/sequence.rb +17 -0
  48. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/static.rb +15 -0
  49. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_assigner.rb +73 -0
  50. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb +54 -0
  51. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/callback.rb +33 -0
  52. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration.rb +22 -0
  53. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/association.rb +25 -0
  54. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/dynamic.rb +25 -0
  55. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/implicit.rb +32 -0
  56. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/static.rb +25 -0
  57. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration_list.rb +48 -0
  58. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/definition.rb +76 -0
  59. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/definition_proxy.rb +159 -0
  60. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/errors.rb +19 -0
  61. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/evaluator.rb +76 -0
  62. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/evaluator_class_definer.rb +34 -0
  63. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/factory.rb +145 -0
  64. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/factory_runner.rb +24 -0
  65. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/find_definitions.rb +25 -0
  66. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/null_factory.rb +15 -0
  67. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/null_object.rb +7 -0
  68. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/registry.rb +46 -0
  69. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/reload.rb +8 -0
  70. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/sequence.rb +24 -0
  71. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/step_definitions.rb +130 -0
  72. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy.rb +32 -0
  73. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/attributes_for.rb +12 -0
  74. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/build.rb +15 -0
  75. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/create.rb +17 -0
  76. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/stub.rb +61 -0
  77. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy_calculator.rb +29 -0
  78. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax.rb +16 -0
  79. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/blueprint.rb +42 -0
  80. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/default.rb +56 -0
  81. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/generate.rb +70 -0
  82. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/make.rb +45 -0
  83. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/methods.rb +136 -0
  84. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/sham.rb +45 -0
  85. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/vintage.rb +130 -0
  86. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/trait.rb +29 -0
  87. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/version.rb +4 -0
  88. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/aliases_spec.rb +19 -0
  89. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attribute_aliases_spec.rb +45 -0
  90. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attribute_existing_on_object_spec.rb +68 -0
  91. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_for_spec.rb +89 -0
  92. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_from_instance_spec.rb +53 -0
  93. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_ordered_spec.rb +51 -0
  94. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_list_spec.rb +41 -0
  95. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_spec.rb +89 -0
  96. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_stubbed_spec.rb +104 -0
  97. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/callbacks_spec.rb +47 -0
  98. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/create_list_spec.rb +82 -0
  99. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/create_spec.rb +117 -0
  100. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/define_child_before_parent_spec.rb +21 -0
  101. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/definition_spec.rb +26 -0
  102. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/definition_without_block_spec.rb +15 -0
  103. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/initialize_with_spec.rb +147 -0
  104. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/modify_factories_spec.rb +184 -0
  105. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/modify_inherited_spec.rb +52 -0
  106. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/nested_attributes_spec.rb +32 -0
  107. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/overrides_spec.rb +61 -0
  108. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/parent_spec.rb +90 -0
  109. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/sequence_spec.rb +33 -0
  110. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/stub_spec.rb +62 -0
  111. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/blueprint_spec.rb +34 -0
  112. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/generate_spec.rb +59 -0
  113. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/make_spec.rb +52 -0
  114. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/sham_spec.rb +43 -0
  115. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/vintage_spec.rb +217 -0
  116. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/traits_spec.rb +421 -0
  117. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/transient_attributes_spec.rb +124 -0
  118. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/aliases_spec.rb +31 -0
  119. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/association_spec.rb +28 -0
  120. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/dynamic_spec.rb +52 -0
  121. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/sequence_spec.rb +16 -0
  122. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/static_spec.rb +19 -0
  123. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute_list_spec.rb +78 -0
  124. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute_spec.rb +16 -0
  125. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/callback_spec.rb +41 -0
  126. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/declaration/implicit_spec.rb +25 -0
  127. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/declaration_list_spec.rb +71 -0
  128. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/definition_proxy_spec.rb +197 -0
  129. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/definition_spec.rb +104 -0
  130. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/evaluator_class_definer_spec.rb +54 -0
  131. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/factory_spec.rb +279 -0
  132. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/find_definitions_spec.rb +110 -0
  133. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/null_factory_spec.rb +13 -0
  134. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/null_object_spec.rb +8 -0
  135. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/registry_spec.rb +81 -0
  136. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/sequence_spec.rb +47 -0
  137. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/attributes_for_spec.rb +18 -0
  138. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/build_spec.rb +7 -0
  139. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/create_spec.rb +13 -0
  140. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/stub_spec.rb +40 -0
  141. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy_calculator_spec.rb +33 -0
  142. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy_spec.rb +21 -0
  143. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl_spec.rb +22 -0
  144. data/vendor/bundle/gems/factory_girl-3.0.0/spec/spec_helper.rb +26 -0
  145. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/macros/define_constant.rb +86 -0
  146. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/callback.rb +9 -0
  147. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/declaration.rb +71 -0
  148. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/delegate.rb +44 -0
  149. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/trait.rb +9 -0
  150. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/shared_examples/strategy.rb +104 -0
  151. data/vendor/bundle/specifications/factory_girl-3.0.0.gemspec +62 -0
  152. metadata +147 -12
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe "a generated attributes hash where order matters" do
4
+ include FactoryGirl::Syntax::Methods
5
+
6
+ before do
7
+ define_model('ParentModel', static: :integer,
8
+ evaluates_first: :integer,
9
+ evaluates_second: :integer,
10
+ evaluates_third: :integer)
11
+
12
+ FactoryGirl.define do
13
+ factory :parent_model do
14
+ evaluates_first { static }
15
+ evaluates_second { evaluates_first }
16
+ evaluates_third { evaluates_second }
17
+
18
+ factory :child_model do
19
+ static 1
20
+ end
21
+ end
22
+
23
+ factory :without_parent, class: ParentModel do
24
+ evaluates_first { static }
25
+ evaluates_second { evaluates_first }
26
+ evaluates_third { evaluates_second }
27
+ static 1
28
+ end
29
+ end
30
+ end
31
+
32
+ context "factory with a parent" do
33
+ subject { FactoryGirl.build(:child_model) }
34
+
35
+ it "assigns attributes in the order they're defined with preference to static attributes" do
36
+ subject[:evaluates_first].should == 1
37
+ subject[:evaluates_second].should == 1
38
+ subject[:evaluates_third].should == 1
39
+ end
40
+ end
41
+
42
+ context "factory without a parent" do
43
+ subject { FactoryGirl.build(:without_parent) }
44
+
45
+ it "assigns attributes in the order they're defined with preference to static attributes without a parent class" do
46
+ subject[:evaluates_first].should == 1
47
+ subject[:evaluates_second].should == 1
48
+ subject[:evaluates_third].should == 1
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe "build multiple instances" do
4
+ before do
5
+ define_model('Post', title: :string)
6
+
7
+ FactoryGirl.define do
8
+ factory(:post) do |post|
9
+ post.title "Through the Looking Glass"
10
+ end
11
+ end
12
+ end
13
+
14
+ context "without default attributes" do
15
+ subject { FactoryGirl.build_list(:post, 20) }
16
+
17
+ its(:length) { should == 20 }
18
+
19
+ it "builds (but doesn't save) all the posts" do
20
+ subject.each do |record|
21
+ record.should be_new_record
22
+ end
23
+ end
24
+
25
+ it "uses the default factory values" do
26
+ subject.each do |record|
27
+ record.title.should == "Through the Looking Glass"
28
+ end
29
+ end
30
+ end
31
+
32
+ context "with default attributes" do
33
+ subject { FactoryGirl.build_list(:post, 20, title: "The Hunting of the Snark") }
34
+
35
+ it "overrides the default values" do
36
+ subject.each do |record|
37
+ record.title.should == "The Hunting of the Snark"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ describe "a built instance" do
4
+ include FactoryGirl::Syntax::Methods
5
+
6
+ before do
7
+ define_model('User')
8
+
9
+ define_model('Post', user_id: :integer) do
10
+ belongs_to :user
11
+ end
12
+
13
+ FactoryGirl.define do
14
+ factory :user
15
+
16
+ factory :post do
17
+ user
18
+ end
19
+ end
20
+ end
21
+
22
+ subject { build(:post) }
23
+
24
+ it "isn't saved" do
25
+ should be_new_record
26
+ end
27
+
28
+ it "assigns and saves associations" do
29
+ subject.user.should be_kind_of(User)
30
+ subject.user.should_not be_new_record
31
+ end
32
+ end
33
+
34
+ describe "a built instance with strategy: :build" do
35
+ include FactoryGirl::Syntax::Methods
36
+
37
+ before do
38
+ define_model('User')
39
+
40
+ define_model('Post', user_id: :integer) do
41
+ belongs_to :user
42
+ end
43
+
44
+ FactoryGirl.define do
45
+ factory :user
46
+
47
+ factory :post do
48
+ association(:user, strategy: :build)
49
+ end
50
+ end
51
+ end
52
+
53
+ subject { build(:post) }
54
+
55
+ it "isn't saved" do
56
+ should be_new_record
57
+ end
58
+
59
+ it "assigns but does not save associations" do
60
+ subject.user.should be_kind_of(User)
61
+ subject.user.should be_new_record
62
+ end
63
+ end
64
+
65
+ describe "calling `build` with a block" do
66
+ include FactoryGirl::Syntax::Methods
67
+
68
+ before do
69
+ define_model('Company', name: :string)
70
+
71
+ FactoryGirl.define do
72
+ factory :company
73
+ end
74
+ end
75
+
76
+ it "passes the built instance" do
77
+ build(:company, name: 'thoughtbot') do |company|
78
+ company.name.should eq('thoughtbot')
79
+ end
80
+ end
81
+
82
+ it "returns the built instance" do
83
+ expected = nil
84
+ build(:company) do |company|
85
+ expected = company
86
+ "hello!"
87
+ end.should == expected
88
+ end
89
+ end
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+
3
+ describe "a generated stub instance" do
4
+ include FactoryGirl::Syntax::Methods
5
+
6
+ before do
7
+ define_model('User')
8
+
9
+ define_model('Post', title: :string,
10
+ body: :string,
11
+ age: :integer,
12
+ user_id: :integer) do
13
+ belongs_to :user
14
+ end
15
+
16
+ FactoryGirl.define do
17
+ factory :user
18
+
19
+ factory :post do
20
+ title { "default title" }
21
+ body { "default body" }
22
+ user
23
+ end
24
+ end
25
+ end
26
+
27
+ subject { build_stubbed(:post, title: 'overridden title') }
28
+
29
+ it "assigns a default attribute" do
30
+ subject.body.should == 'default body'
31
+ end
32
+
33
+ it "assigns an overridden attribute" do
34
+ subject.title.should == 'overridden title'
35
+ end
36
+
37
+ it "assigns associations" do
38
+ subject.user.should_not be_nil
39
+ end
40
+
41
+ it "has an id" do
42
+ subject.id.should > 0
43
+ end
44
+
45
+ it "generates unique ids" do
46
+ other_stub = build_stubbed(:post)
47
+ subject.id.should_not == other_stub.id
48
+ end
49
+
50
+ it "isn't a new record" do
51
+ should_not be_new_record
52
+ end
53
+
54
+ it "disables connection" do
55
+ lambda { subject.connection }.should raise_error(RuntimeError)
56
+ end
57
+
58
+ it "disables update_attribute" do
59
+ lambda { subject.update_attribute(:title, "value") }.should raise_error(RuntimeError)
60
+ end
61
+
62
+ it "disables reload" do
63
+ lambda { subject.reload }.should raise_error(RuntimeError)
64
+ end
65
+
66
+ it "disables destroy" do
67
+ lambda { subject.destroy }.should raise_error(RuntimeError)
68
+ end
69
+
70
+ it "disables save" do
71
+ lambda { subject.save }.should raise_error(RuntimeError)
72
+ end
73
+
74
+ it "disables increment" do
75
+ lambda { subject.increment!(:age) }.should raise_error(RuntimeError)
76
+ end
77
+ end
78
+
79
+ describe "calling `build_stubbed` with a block" do
80
+ include FactoryGirl::Syntax::Methods
81
+
82
+ before do
83
+ define_model('Company', name: :string)
84
+
85
+ FactoryGirl.define do
86
+ factory :company
87
+ end
88
+ end
89
+
90
+ it "passes the stub instance" do
91
+ build_stubbed(:company, name: 'thoughtbot') do |company|
92
+ company.name.should eq('thoughtbot')
93
+ expect { company.save }.to raise_error(RuntimeError)
94
+ end
95
+ end
96
+
97
+ it "returns the stub instance" do
98
+ expected = nil
99
+ build_stubbed(:company) do |company|
100
+ expected = company
101
+ "hello!"
102
+ end.should == expected
103
+ end
104
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe "callbacks" do
4
+ before do
5
+ define_model("User", first_name: :string, last_name: :string)
6
+
7
+ FactoryGirl.define do
8
+ factory :user_with_callbacks, class: :user do
9
+ after_stub { |user| user.first_name = 'Stubby' }
10
+ after_build { |user| user.first_name = 'Buildy' }
11
+ after_create { |user| user.last_name = 'Createy' }
12
+ end
13
+
14
+ factory :user_with_inherited_callbacks, parent: :user_with_callbacks do
15
+ after_stub { |user| user.last_name = 'Double-Stubby' }
16
+ after_build { |user| user.first_name = 'Child-Buildy' }
17
+ end
18
+ end
19
+ end
20
+
21
+ it "runs the after_stub callback when stubbing" do
22
+ user = FactoryGirl.build_stubbed(:user_with_callbacks)
23
+ user.first_name.should == 'Stubby'
24
+ end
25
+
26
+ it "runs the after_build callback when building" do
27
+ user = FactoryGirl.build(:user_with_callbacks)
28
+ user.first_name.should == 'Buildy'
29
+ end
30
+
31
+ it "runs both the after_build and after_create callbacks when creating" do
32
+ user = FactoryGirl.create(:user_with_callbacks)
33
+ user.first_name.should == 'Buildy'
34
+ user.last_name.should == 'Createy'
35
+ end
36
+
37
+ it "runs both the after_stub callback on the factory and the inherited after_stub callback" do
38
+ user = FactoryGirl.build_stubbed(:user_with_inherited_callbacks)
39
+ user.first_name.should == 'Stubby'
40
+ user.last_name.should == 'Double-Stubby'
41
+ end
42
+
43
+ it "runs child callback after parent callback" do
44
+ user = FactoryGirl.build(:user_with_inherited_callbacks)
45
+ user.first_name.should == 'Child-Buildy'
46
+ end
47
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe "create multiple instances" do
4
+ before do
5
+ define_model('Post', title: :string)
6
+
7
+ FactoryGirl.define do
8
+ factory(:post) do |post|
9
+ post.title "Through the Looking Glass"
10
+ end
11
+ end
12
+ end
13
+
14
+ context "without default attributes" do
15
+ subject { FactoryGirl.create_list(:post, 20) }
16
+
17
+ its(:length) { should == 20 }
18
+
19
+ it "creates all the posts" do
20
+ subject.each do |record|
21
+ record.should_not be_new_record
22
+ end
23
+ end
24
+
25
+ it "uses the default factory values" do
26
+ subject.each do |record|
27
+ record.title.should == "Through the Looking Glass"
28
+ end
29
+ end
30
+ end
31
+
32
+ context "with default attributes" do
33
+ subject { FactoryGirl.create_list(:post, 20, title: "The Hunting of the Snark") }
34
+
35
+ it "overrides the default values" do
36
+ subject.each do |record|
37
+ record.title.should == "The Hunting of the Snark"
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "multiple creates and ignored attributes to dynamically build attribute lists" do
44
+ before do
45
+ define_model('User', name: :string) do
46
+ has_many :posts
47
+ end
48
+
49
+ define_model('Post', title: :string, user_id: :integer) do
50
+ belongs_to :user
51
+ end
52
+
53
+ FactoryGirl.define do
54
+ factory :post do
55
+ title "Through the Looking Glass"
56
+ user
57
+ end
58
+
59
+ factory :user do
60
+ name "John Doe"
61
+
62
+ factory :user_with_posts do
63
+ ignore do
64
+ posts_count 5
65
+ end
66
+
67
+ after_create do |user, evaluator|
68
+ FactoryGirl.create_list(:post, evaluator.posts_count, user: user)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ it "generates the correct number of posts" do
76
+ FactoryGirl.create(:user_with_posts).posts.length.should == 5
77
+ end
78
+
79
+ it "allows the number of posts to be modified" do
80
+ FactoryGirl.create(:user_with_posts, posts_count: 2).posts.length.should == 2
81
+ end
82
+ end
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ describe "a created instance" do
4
+ include FactoryGirl::Syntax::Methods
5
+
6
+ before do
7
+ define_model('User')
8
+
9
+ define_model('Post', user_id: :integer) do
10
+ belongs_to :user
11
+ end
12
+
13
+ FactoryGirl.define do
14
+ factory :user
15
+
16
+ factory :post do
17
+ user
18
+ end
19
+ end
20
+ end
21
+
22
+ subject { create('post') }
23
+
24
+ it "saves" do
25
+ should_not be_new_record
26
+ end
27
+
28
+ it "assigns and saves associations" do
29
+ subject.user.should be_kind_of(User)
30
+ subject.user.should_not be_new_record
31
+ end
32
+ end
33
+
34
+ describe "a created instance, specifying strategy: :build" do
35
+ include FactoryGirl::Syntax::Methods
36
+
37
+ before do
38
+ define_model('User')
39
+
40
+ define_model('Post', user_id: :integer) do
41
+ belongs_to :user
42
+ end
43
+
44
+ FactoryGirl.define do
45
+ factory :user
46
+
47
+ factory :post do
48
+ association(:user, strategy: :build)
49
+ end
50
+ end
51
+ end
52
+
53
+ subject { create(:post) }
54
+
55
+ it "saves associations (strategy: :build only affects build, not create)" do
56
+ subject.user.should be_kind_of(User)
57
+ subject.user.should_not be_new_record
58
+ end
59
+ end
60
+
61
+ describe "a custom create" do
62
+ include FactoryGirl::Syntax::Methods
63
+
64
+ before do
65
+ define_class('User') do
66
+ def initialize
67
+ @persisted = false
68
+ end
69
+
70
+ def persist
71
+ @persisted = true
72
+ end
73
+
74
+ def persisted?
75
+ @persisted
76
+ end
77
+ end
78
+
79
+ FactoryGirl.define do
80
+ factory :user do
81
+ to_create do |user|
82
+ user.persist
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ it "uses the custom create block instead of save" do
89
+ FactoryGirl.create(:user).should be_persisted
90
+ end
91
+ end
92
+
93
+ describe "calling `create` with a block" do
94
+ include FactoryGirl::Syntax::Methods
95
+
96
+ before do
97
+ define_model('Company', name: :string)
98
+
99
+ FactoryGirl.define do
100
+ factory :company
101
+ end
102
+ end
103
+
104
+ it "passes the created instance" do
105
+ create(:company, name: 'thoughtbot') do |company|
106
+ company.name.should eq('thoughtbot')
107
+ end
108
+ end
109
+
110
+ it "returns the created instance" do
111
+ expected = nil
112
+ create(:company) do |company|
113
+ expected = company
114
+ "hello!"
115
+ end.should == expected
116
+ end
117
+ end