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,124 @@
1
+ require "spec_helper"
2
+
3
+ describe "transient attributes" do
4
+ before do
5
+ define_model("User", name: :string, email: :string)
6
+
7
+ FactoryGirl.define do
8
+ sequence(:name) {|n| "John #{n}" }
9
+
10
+ factory :user do
11
+ ignore do
12
+ four { 2 + 2 }
13
+ rockstar true
14
+ upcased false
15
+ end
16
+
17
+ name { "#{FactoryGirl.generate(:name)}#{" - Rockstar" if rockstar}" }
18
+ email { "#{name.downcase}#{four}@example.com" }
19
+
20
+ after_create do |user, evaluator|
21
+ user.name.upcase! if evaluator.upcased
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ context "returning attributes for a factory" do
28
+ subject { FactoryGirl.attributes_for(:user, rockstar: true) }
29
+ it { should_not have_key(:four) }
30
+ it { should_not have_key(:rockstar) }
31
+ it { should_not have_key(:upcased) }
32
+ it { should have_key(:name) }
33
+ it { should have_key(:email) }
34
+ end
35
+
36
+ context "with a transient variable assigned" do
37
+ let(:rockstar) { FactoryGirl.create(:user, rockstar: true, four: "1234") }
38
+ let(:rockstar_with_name) { FactoryGirl.create(:user, name: "Jane Doe", rockstar: true) }
39
+ let(:upcased_rockstar) { FactoryGirl.create(:user, rockstar: true, upcased: true) }
40
+ let(:groupie) { FactoryGirl.create(:user, rockstar: false) }
41
+
42
+ it "generates the correct attributes on a rockstar" do
43
+ rockstar.name.should == "John 1 - Rockstar"
44
+ rockstar.email.should == "john 1 - rockstar1234@example.com"
45
+ end
46
+
47
+ it "generates the correct attributes on an upcased rockstar" do
48
+ upcased_rockstar.name.should == "JOHN 1 - ROCKSTAR"
49
+ upcased_rockstar.email.should == "john 1 - rockstar4@example.com"
50
+ end
51
+
52
+ it "generates the correct attributes on a groupie" do
53
+ groupie.name.should == "John 1"
54
+ groupie.email.should == "john 14@example.com"
55
+ end
56
+
57
+ it "generates the correct attributes on a rockstar with a name" do
58
+ rockstar_with_name.name.should == "Jane Doe"
59
+ rockstar_with_name.email.should == "jane doe4@example.com"
60
+ end
61
+ end
62
+
63
+ context "without transient variables assigned" do
64
+ let(:rockstar) { FactoryGirl.create(:user) }
65
+
66
+ it "uses the default value of the attribute" do
67
+ rockstar.name.should == "John 1 - Rockstar"
68
+ end
69
+ end
70
+ end
71
+
72
+ describe "transient sequences" do
73
+ before do
74
+ define_model("User", name: :string)
75
+
76
+ FactoryGirl.define do
77
+ factory :user do
78
+ ignore do
79
+ sequence(:counter)
80
+ end
81
+
82
+ name { "John Doe #{counter}" }
83
+ end
84
+ end
85
+ end
86
+
87
+ it "increments sequences correctly" do
88
+ FactoryGirl.build(:user).name.should == "John Doe 1"
89
+ FactoryGirl.build(:user).name.should == "John Doe 2"
90
+ end
91
+ end
92
+
93
+ describe "assigning values from a transient attribute" do
94
+ before do
95
+ define_class("User") do
96
+ attr_accessor :foo_id, :foo_name
97
+ end
98
+
99
+ define_class("Foo") do
100
+ attr_accessor :id, :name
101
+ def initialize(id, name)
102
+ @id = id
103
+ @name = name
104
+ end
105
+ end
106
+
107
+ FactoryGirl.define do
108
+ factory :user do
109
+ ignore do
110
+ foo { Foo.new('id-of-foo', 'name-of-foo')}
111
+ end
112
+
113
+ foo_id { foo.id }
114
+ foo_name { foo.name }
115
+ end
116
+ end
117
+ end
118
+
119
+ it "does not ignore an _id attribute that is an alias for a transient attribute" do
120
+ user = FactoryGirl.build(:user, :foo => Foo.new('passed-in-id-of-foo', 'passed-in-name-of-foo'))
121
+ user.foo_id.should == 'passed-in-id-of-foo'
122
+ user.foo_name.should == 'passed-in-name-of-foo'
123
+ end
124
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl, "aliases" do
4
+ context "aliases for an attribute" do
5
+ subject { FactoryGirl.aliases_for(:test) }
6
+ it { should include(:test) }
7
+ it { should include(:test_id) }
8
+ end
9
+
10
+ context "aliases for a foreign key" do
11
+ subject { FactoryGirl.aliases_for(:test_id) }
12
+ it { should include(:test) }
13
+ it { should include(:test_id) }
14
+ end
15
+
16
+ context "aliases for an attribute starting with an underscore" do
17
+ subject { FactoryGirl.aliases_for(:_id) }
18
+ it { should_not include(:id) }
19
+ end
20
+ end
21
+
22
+ describe Factory, "after defining an alias" do
23
+ before do
24
+ FactoryGirl.aliases << [/(.*)_suffix/, '\1']
25
+ end
26
+
27
+ subject { FactoryGirl.aliases_for(:test_suffix) }
28
+
29
+ it { should include(:test) }
30
+ it { should include(:test_suffix_id) }
31
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute::Association do
4
+ let(:name) { :author }
5
+ let(:factory) { :user }
6
+ let(:overrides) { { first_name: "John" } }
7
+ let(:association) { stub("association") }
8
+
9
+ subject { FactoryGirl::Attribute::Association.new(name, factory, overrides) }
10
+ before { subject.stubs(association: association) }
11
+
12
+ it { should be_association }
13
+ its(:name) { should == name }
14
+
15
+ it "builds the association when calling the proc" do
16
+ subject.to_proc.call.should == association
17
+ end
18
+
19
+ it "builds the association when calling the proc" do
20
+ subject.to_proc.call
21
+ subject.should have_received(:association).with(factory, overrides)
22
+ end
23
+ end
24
+
25
+ describe FactoryGirl::Attribute::Association, "with a string name" do
26
+ subject { FactoryGirl::Attribute::Association.new("name", :user, {}) }
27
+ its(:name) { should == :name }
28
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute::Dynamic do
4
+ let(:name) { :first_name }
5
+ let(:block) { lambda { } }
6
+
7
+ subject { FactoryGirl::Attribute::Dynamic.new(name, false, block) }
8
+
9
+ its(:name) { should == name }
10
+
11
+ context "with a block returning a static value" do
12
+ let(:block) { lambda { "value" } }
13
+
14
+ it "returns the value when executing the proc" do
15
+ subject.to_proc.call.should == "value"
16
+ end
17
+ end
18
+
19
+ context "with a block returning its block-level variable" do
20
+ let(:block) { lambda {|thing| thing } }
21
+
22
+ it "returns self when executing the proc" do
23
+ subject.to_proc.call.should == subject
24
+ end
25
+ end
26
+
27
+ context "with a block referencing an attribute on the attribute" do
28
+ let(:block) { lambda { attribute_defined_on_attribute } }
29
+ let(:result) { "other attribute value" }
30
+
31
+ before do
32
+ subject.stubs(attribute_defined_on_attribute: result)
33
+ end
34
+
35
+ it "evaluates the attribute from the attribute" do
36
+ subject.to_proc.call.should == result
37
+ end
38
+ end
39
+
40
+ context "with a block returning a sequence" do
41
+ let(:block) { lambda { FactoryGirl.register_sequence(FactoryGirl::Sequence.new(:email, 1) {|n| "foo#{n}" }) } }
42
+
43
+ it "raises a sequence abuse error" do
44
+ expect { subject.to_proc.call }.to raise_error(FactoryGirl::SequenceAbuseError)
45
+ end
46
+ end
47
+ end
48
+
49
+ describe FactoryGirl::Attribute::Dynamic, "with a string name" do
50
+ subject { FactoryGirl::Attribute::Dynamic.new("name", false, lambda { } ) }
51
+ its(:name) { should == :name }
52
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute::Sequence do
4
+ let(:sequence_name) { :name }
5
+ let(:name) { :first_name }
6
+ let(:sequence) { FactoryGirl::Sequence.new(sequence_name, 5) { |n| "Name #{n}" } }
7
+
8
+ subject { FactoryGirl::Attribute::Sequence.new(name, sequence_name, false) }
9
+ before { FactoryGirl.register_sequence(sequence) }
10
+
11
+ its(:name) { should == name }
12
+
13
+ it "assigns the next value in the sequence" do
14
+ subject.to_proc.call.should == "Name 5"
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute::Static do
4
+ let(:name) { :first_name }
5
+ let(:value) { "John" }
6
+
7
+ subject { FactoryGirl::Attribute::Static.new(name, value, false) }
8
+
9
+ its(:name) { should == name }
10
+
11
+ it "returns the value when executing the proc" do
12
+ subject.to_proc.call.should == value
13
+ end
14
+ end
15
+
16
+ describe FactoryGirl::Attribute::Static, "with a string name" do
17
+ subject { FactoryGirl::Attribute::Static.new("name", nil, false) }
18
+ its(:name) { should == :name }
19
+ end
@@ -0,0 +1,78 @@
1
+ require "spec_helper"
2
+
3
+ describe FactoryGirl::AttributeList, "#define_attribute" do
4
+ let(:static_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "value", false) }
5
+ let(:dynamic_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, lambda {|u| "#{u.full_name}@example.com" }) }
6
+
7
+ it "maintains a list of attributes" do
8
+ subject.define_attribute(static_attribute)
9
+ subject.to_a.should == [static_attribute]
10
+
11
+ subject.define_attribute(dynamic_attribute)
12
+ subject.to_a.should == [static_attribute, dynamic_attribute]
13
+ end
14
+
15
+ it "returns the attribute" do
16
+ subject.define_attribute(static_attribute).should == static_attribute
17
+ subject.define_attribute(dynamic_attribute).should == dynamic_attribute
18
+ end
19
+
20
+ it "raises if an attribute has already been defined" do
21
+ expect {
22
+ 2.times { subject.define_attribute(static_attribute) }
23
+ }.to raise_error(FactoryGirl::AttributeDefinitionError, "Attribute already defined: full_name")
24
+ end
25
+ end
26
+
27
+ describe FactoryGirl::AttributeList, "#define_attribute with a named attribute list" do
28
+ subject { FactoryGirl::AttributeList.new(:author) }
29
+
30
+ let(:association_with_same_name) { FactoryGirl::Attribute::Association.new(:author, :author, {}) }
31
+ let(:association_with_different_name) { FactoryGirl::Attribute::Association.new(:author, :post, {}) }
32
+
33
+ it "raises when the attribute is a self-referencing association" do
34
+ expect { subject.define_attribute(association_with_same_name) }.to raise_error(FactoryGirl::AssociationDefinitionError, "Self-referencing association 'author' in 'author'")
35
+ end
36
+
37
+ it "does not raise when the attribute is not a self-referencing association" do
38
+ expect { subject.define_attribute(association_with_different_name) }.to_not raise_error
39
+ end
40
+ end
41
+
42
+ describe FactoryGirl::AttributeList, "#apply_attributes" do
43
+ let(:full_name_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "John Adams", false) }
44
+ let(:city_attribute) { FactoryGirl::Attribute::Static.new(:city, "Boston", false) }
45
+ let(:email_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, lambda {|model| "#{model.full_name}@example.com" }) }
46
+ let(:login_attribute) { FactoryGirl::Attribute::Dynamic.new(:login, false, lambda {|model| "username-#{model.full_name}" }) }
47
+
48
+ def list(*attributes)
49
+ FactoryGirl::AttributeList.new.tap do |list|
50
+ attributes.each { |attribute| list.define_attribute(attribute) }
51
+ end
52
+ end
53
+
54
+ it "adds attributes in the order defined regardless of attribute type" do
55
+ subject.define_attribute(full_name_attribute)
56
+ subject.define_attribute(login_attribute)
57
+ subject.apply_attributes(list(city_attribute, email_attribute))
58
+ subject.to_a.should == [full_name_attribute, login_attribute, city_attribute, email_attribute]
59
+ end
60
+ end
61
+
62
+ describe FactoryGirl::AttributeList, "#associations" do
63
+ let(:full_name_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "value", false) }
64
+ let(:email_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, lambda {|u| "#{u.full_name}@example.com" }) }
65
+ let(:author_attribute) { FactoryGirl::Attribute::Association.new(:author, :user, {}) }
66
+ let(:profile_attribute) { FactoryGirl::Attribute::Association.new(:profile, :profile, {}) }
67
+
68
+ before do
69
+ subject.define_attribute(full_name_attribute)
70
+ subject.define_attribute(email_attribute)
71
+ subject.define_attribute(author_attribute)
72
+ subject.define_attribute(profile_attribute)
73
+ end
74
+
75
+ it "returns associations" do
76
+ subject.associations.should == [author_attribute, profile_attribute]
77
+ end
78
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute do
4
+ let(:name) { "user" }
5
+ subject { FactoryGirl::Attribute.new(name, false) }
6
+
7
+ its(:name) { should == name.to_sym }
8
+ it { should_not be_association }
9
+
10
+ it "raises an error when defining an attribute writer" do
11
+ error_message = %{factory_girl uses 'f.test value' syntax rather than 'f.test = value'}
12
+ expect {
13
+ FactoryGirl::Attribute.new('test=', false)
14
+ }.to raise_error(FactoryGirl::AttributeDefinitionError, error_message)
15
+ end
16
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Callback do
4
+ it "has a name" do
5
+ FactoryGirl::Callback.new(:after_create, lambda {}).name.should == :after_create
6
+ end
7
+
8
+ it "converts strings to symbols" do
9
+ FactoryGirl::Callback.new("after_create", lambda {}).name.should == :after_create
10
+ end
11
+
12
+ it "runs its block with no parameters" do
13
+ ran_with = nil
14
+ FactoryGirl::Callback.new(:after_create, lambda { ran_with = [] }).run(:one, :two)
15
+ ran_with.should == []
16
+ end
17
+
18
+ it "runs its block with one parameter" do
19
+ ran_with = nil
20
+ FactoryGirl::Callback.new(:after_create, lambda { |one| ran_with = [one] }).run(:one, :two)
21
+ ran_with.should == [:one]
22
+ end
23
+
24
+ it "runs its block with two parameters" do
25
+ ran_with = nil
26
+ FactoryGirl::Callback.new(:after_create, lambda { |one, two| ran_with = [one, two] }).run(:one, :two)
27
+ ran_with.should == [:one, :two]
28
+ end
29
+
30
+ it "allows valid callback names to be assigned" do
31
+ FactoryGirl.callback_names.each do |callback_name|
32
+ expect { FactoryGirl::Callback.new(callback_name, lambda {}) }.
33
+ to_not raise_error(FactoryGirl::InvalidCallbackNameError)
34
+ end
35
+ end
36
+
37
+ it "raises if an invalid callback name is assigned" do
38
+ expect { FactoryGirl::Callback.new(:magic_fairies, lambda {}) }.
39
+ to raise_error(FactoryGirl::InvalidCallbackNameError, /magic_fairies is not a valid callback name/)
40
+ end
41
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Declaration::Implicit do
4
+ let(:name) { :author }
5
+ let(:declaration) { FactoryGirl::Declaration::Implicit.new(name) }
6
+ subject { declaration.to_attributes.first }
7
+
8
+ context "with a known factory" do
9
+ before do
10
+ FactoryGirl.factories.stubs(:registered? => true)
11
+ end
12
+
13
+ it { should be_association }
14
+ its(:factory) { should == name }
15
+ end
16
+
17
+ context "with a known sequence" do
18
+ before do
19
+ FactoryGirl.sequences.stubs(:registered? => true)
20
+ end
21
+
22
+ it { should_not be_association }
23
+ it { should be_a(FactoryGirl::Attribute::Sequence) }
24
+ end
25
+ end