factory_girl 2.0.4 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CONTRIBUTION_GUIDELINES.md +1 -0
- data/GETTING_STARTED.md +28 -4
- data/Gemfile +2 -1
- data/Gemfile.lock +6 -2
- data/features/factory_girl_steps.feature +17 -0
- data/features/step_definitions/database_steps.rb +22 -0
- data/features/support/factories.rb +13 -0
- data/features/support/test.db +0 -0
- data/lib/factory_girl/aliases.rb +1 -3
- data/lib/factory_girl/attribute.rb +16 -8
- data/lib/factory_girl/attribute/association.rb +0 -3
- data/lib/factory_girl/attribute/callback.rb +0 -2
- data/lib/factory_girl/attribute/dynamic.rb +1 -3
- data/lib/factory_girl/attribute/static.rb +1 -1
- data/lib/factory_girl/attribute_list.rb +30 -9
- data/lib/factory_girl/factory.rb +1 -1
- data/lib/factory_girl/proxy.rb +7 -5
- data/lib/factory_girl/proxy/attributes_for.rb +8 -3
- data/lib/factory_girl/proxy/build.rb +12 -3
- data/lib/factory_girl/proxy/stub.rb +12 -3
- data/lib/factory_girl/sequence.rb +2 -2
- data/lib/factory_girl/step_definitions.rb +3 -2
- data/lib/factory_girl/version.rb +1 -1
- data/spec/acceptance/attribute_aliases_spec.rb +0 -1
- data/spec/acceptance/attributes_for_spec.rb +0 -1
- data/spec/acceptance/attributes_ordered_spec.rb +24 -6
- data/spec/acceptance/build_list_spec.rb +0 -1
- data/spec/acceptance/build_spec.rb +0 -1
- data/spec/acceptance/build_stubbed_spec.rb +0 -1
- data/spec/acceptance/callbacks_spec.rb +0 -1
- data/spec/acceptance/create_list_spec.rb +0 -1
- data/spec/acceptance/create_spec.rb +0 -1
- data/spec/acceptance/default_strategy_spec.rb +0 -1
- data/spec/acceptance/definition_spec.rb +0 -1
- data/spec/acceptance/definition_without_block_spec.rb +0 -1
- data/spec/acceptance/overrides_spec.rb +0 -1
- data/spec/acceptance/parent_spec.rb +18 -1
- data/spec/acceptance/sequence_spec.rb +0 -1
- data/spec/acceptance/syntax/blueprint_spec.rb +0 -1
- data/spec/acceptance/syntax/generate_spec.rb +0 -1
- data/spec/acceptance/syntax/make_spec.rb +0 -1
- data/spec/acceptance/syntax/sham_spec.rb +0 -1
- data/spec/acceptance/syntax/vintage_spec.rb +27 -29
- data/spec/acceptance/traits_spec.rb +0 -1
- data/spec/acceptance/transient_attributes_spec.rb +68 -0
- data/spec/factory_girl/aliases_spec.rb +19 -21
- data/spec/factory_girl/attribute/association_spec.rb +16 -24
- data/spec/factory_girl/attribute/callback_spec.rb +14 -15
- data/spec/factory_girl/attribute/dynamic_spec.rb +41 -45
- data/spec/factory_girl/attribute/implicit_spec.rb +18 -30
- data/spec/factory_girl/attribute/sequence_spec.rb +11 -13
- data/spec/factory_girl/attribute/static_spec.rb +13 -20
- data/spec/factory_girl/attribute_list_spec.rb +9 -1
- data/spec/factory_girl/attribute_spec.rb +17 -28
- data/spec/factory_girl/definition_proxy_spec.rb +131 -82
- data/spec/factory_girl/deprecated_spec.rb +15 -36
- data/spec/factory_girl/factory_spec.rb +106 -135
- data/spec/factory_girl/find_definitions_spec.rb +7 -6
- data/spec/factory_girl/proxy/attributes_for_spec.rb +23 -32
- data/spec/factory_girl/proxy/build_spec.rb +6 -80
- data/spec/factory_girl/proxy/create_spec.rb +24 -89
- data/spec/factory_girl/proxy/stub_spec.rb +14 -73
- data/spec/factory_girl/proxy_spec.rb +53 -53
- data/spec/factory_girl/registry_spec.rb +13 -29
- data/spec/factory_girl/sequence_spec.rb +24 -72
- data/spec/factory_girl_spec.rb +10 -5
- data/spec/spec_helper.rb +5 -79
- data/spec/support/macros/define_constant.rb +86 -0
- data/spec/support/shared_examples/proxy.rb +99 -0
- metadata +34 -20
- data/spec/acceptance/acceptance_helper.rb +0 -11
@@ -2,15 +2,16 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
share_examples_for "finds definitions" do
|
4
4
|
before do
|
5
|
-
|
5
|
+
FactoryGirl.stubs(:load)
|
6
6
|
FactoryGirl.find_definitions
|
7
7
|
end
|
8
|
+
|
8
9
|
subject { FactoryGirl }
|
9
10
|
end
|
10
11
|
|
11
12
|
RSpec::Matchers.define :load_definitions_from do |file|
|
12
13
|
match do |given|
|
13
|
-
@has_received = have_received
|
14
|
+
@has_received = have_received(:load).with(File.expand_path(file))
|
14
15
|
@has_received.matches?(given)
|
15
16
|
end
|
16
17
|
|
@@ -23,7 +24,6 @@ RSpec::Matchers.define :load_definitions_from do |file|
|
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
26
|
-
|
27
27
|
describe "definition loading" do
|
28
28
|
def self.in_directory_with_files(*files)
|
29
29
|
before do
|
@@ -79,10 +79,11 @@ describe "definition loading" do
|
|
79
79
|
in_directory_with_files File.join(dir, 'factories', 'b.rb'),
|
80
80
|
File.join(dir, 'factories', 'a.rb')
|
81
81
|
it "should load the files in the right order" do
|
82
|
-
|
83
|
-
|
82
|
+
FactoryGirl.stubs(:load)
|
83
|
+
sorted_load_order = sequence("load order")
|
84
|
+
FactoryGirl.expects(:load).with(includes("a.rb")).in_sequence(sorted_load_order)
|
85
|
+
FactoryGirl.expects(:load).with(includes("b.rb")).in_sequence(sorted_load_order)
|
84
86
|
FactoryGirl.find_definitions
|
85
|
-
@loaded.should == ["a.rb", "b.rb"]
|
86
87
|
end
|
87
88
|
end
|
88
89
|
|
@@ -1,51 +1,42 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Proxy::AttributesFor do
|
4
|
-
|
5
|
-
@proxy = FactoryGirl::Proxy::AttributesFor.new(@class)
|
6
|
-
end
|
4
|
+
let(:proxy_class) { stub("class") }
|
7
5
|
|
8
|
-
|
9
|
-
before do
|
10
|
-
stub(FactoryGirl).create
|
11
|
-
@proxy.associate(:owner, :user, {})
|
12
|
-
end
|
6
|
+
subject { FactoryGirl::Proxy::AttributesFor.new(proxy_class) }
|
13
7
|
|
14
|
-
|
15
|
-
(@proxy.result(nil).key?(:owner)).should_not be
|
16
|
-
end
|
17
|
-
end
|
8
|
+
it_should_behave_like "proxy without association support"
|
18
9
|
|
19
|
-
it "
|
20
|
-
|
10
|
+
it "returns a hash when asked for the result" do
|
11
|
+
subject.result(nil).should be_kind_of(Hash)
|
21
12
|
end
|
22
13
|
|
23
|
-
|
24
|
-
|
25
|
-
@proxy.association(:user).should be_nil
|
26
|
-
FactoryGirl.should have_received.create.never
|
27
|
-
end
|
14
|
+
context "after associating a factory" do
|
15
|
+
let(:attribute) { :owner }
|
28
16
|
|
29
|
-
|
30
|
-
@proxy.set(:association, 'x')
|
31
|
-
@proxy.association(:user).should be_nil
|
32
|
-
end
|
17
|
+
before { subject.associate(attribute, :user, {}) }
|
33
18
|
|
34
|
-
|
35
|
-
|
19
|
+
it "doesn't set that key in the resulting hash" do
|
20
|
+
subject.result(nil).should_not have_key(attribute)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns nil when asked for that attribute" do
|
24
|
+
subject.get(attribute).should be_nil
|
25
|
+
end
|
36
26
|
end
|
37
27
|
|
38
28
|
describe "after setting an attribute" do
|
39
|
-
|
40
|
-
|
41
|
-
|
29
|
+
let(:attribute) { :attribute }
|
30
|
+
let(:value) { "value" }
|
31
|
+
|
32
|
+
before { subject.set(attribute, value) }
|
42
33
|
|
43
|
-
it "
|
44
|
-
|
34
|
+
it "sets that value in the resulting hash" do
|
35
|
+
subject.result(nil)[attribute].should == value
|
45
36
|
end
|
46
37
|
|
47
|
-
it "
|
48
|
-
|
38
|
+
it "returns that value when asked for that attribute" do
|
39
|
+
subject.get(attribute).should == value
|
49
40
|
end
|
50
41
|
end
|
51
42
|
end
|
@@ -1,86 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Proxy::Build do
|
4
|
-
|
5
|
-
|
6
|
-
@instance = "built-instance"
|
4
|
+
let(:instance) { stub("built-instance") }
|
5
|
+
let(:proxy_class) { stub("class", :new => instance) }
|
7
6
|
|
8
|
-
|
9
|
-
stub(@instance).attribute { 'value' }
|
10
|
-
stub(@instance, :attribute=)
|
11
|
-
stub(@instance, :owner=)
|
7
|
+
subject { FactoryGirl::Proxy::Build.new(proxy_class) }
|
12
8
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
it "should instantiate the class" do
|
17
|
-
@class.should have_received.new
|
18
|
-
end
|
19
|
-
|
20
|
-
describe "when asked to associate with another factory" do
|
21
|
-
before do
|
22
|
-
@association = "associated-instance"
|
23
|
-
@associated_factory = "associated-factory"
|
24
|
-
stub(FactoryGirl).factory_by_name { @associated_factory }
|
25
|
-
stub(@associated_factory).run { @association }
|
26
|
-
@overrides = { 'attr' => 'value' }
|
27
|
-
@proxy.associate(:owner, :user, @overrides)
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should create the associated instance" do
|
31
|
-
@associated_factory.should have_received.run(FactoryGirl::Proxy::Create, @overrides)
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should set the associated instance" do
|
35
|
-
@instance.should have_received.method_missing(:owner=, @association)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
it "should run create when building an association" do
|
40
|
-
association = "associated-instance"
|
41
|
-
associated_factory = "associated-factory"
|
42
|
-
stub(FactoryGirl).factory_by_name { associated_factory }
|
43
|
-
stub(associated_factory).run { association }
|
44
|
-
overrides = { 'attr' => 'value' }
|
45
|
-
@proxy.association(:user, overrides).should == association
|
46
|
-
associated_factory.should have_received.run(FactoryGirl::Proxy::Create, overrides)
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should return the built instance when asked for the result" do
|
50
|
-
@proxy.result(nil).should == @instance
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should run the :after_build callback when retrieving the result" do
|
54
|
-
spy = Object.new
|
55
|
-
stub(spy).foo
|
56
|
-
@proxy.add_callback(:after_build, proc{ spy.foo })
|
57
|
-
@proxy.result(nil)
|
58
|
-
spy.should have_received.foo
|
59
|
-
end
|
60
|
-
|
61
|
-
describe "when setting an attribute" do
|
62
|
-
before do
|
63
|
-
stub(@instance).attribute = 'value'
|
64
|
-
@proxy.set(:attribute, 'value')
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should set that value" do
|
68
|
-
@instance.should have_received.method_missing(:attribute=, 'value')
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
describe "when getting an attribute" do
|
73
|
-
before do
|
74
|
-
@result = @proxy.get(:attribute)
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should ask the built class for the value" do
|
78
|
-
@instance.should have_received.attribute
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should return the value for that attribute" do
|
82
|
-
@result.should == 'value'
|
83
|
-
end
|
84
|
-
end
|
9
|
+
it_should_behave_like "proxy with association support", FactoryGirl::Proxy::Create
|
10
|
+
it_should_behave_like "proxy with standard getters and setters", :attribute_name, "attribute value!"
|
11
|
+
it_should_behave_like "proxy with callbacks", :after_build
|
85
12
|
end
|
86
|
-
|
@@ -1,107 +1,42 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Proxy::Create do
|
4
|
-
before do
|
5
|
-
@class = Class.new
|
6
|
-
@instance = "built-instance"
|
7
4
|
|
8
|
-
|
9
|
-
|
10
|
-
stub(@instance, :attribute=)
|
11
|
-
stub(@instance, :owner=)
|
12
|
-
stub(@instance).save!
|
5
|
+
let(:instance) { stub("created-instance", :save! => true) }
|
6
|
+
let(:proxy_class) { stub("class", :new => instance) }
|
13
7
|
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should instantiate the class" do
|
18
|
-
@class.should have_received.new
|
19
|
-
end
|
8
|
+
subject { FactoryGirl::Proxy::Create.new(proxy_class) }
|
20
9
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
stub(FactoryGirl).factory_by_name { @associated_factory }
|
26
|
-
stub(@associated_factory).run { @association }
|
27
|
-
@overrides = { 'attr' => 'value' }
|
28
|
-
@proxy.associate(:owner, :user, @overrides)
|
29
|
-
end
|
10
|
+
it_should_behave_like "proxy with association support", FactoryGirl::Proxy::Create
|
11
|
+
it_should_behave_like "proxy with standard getters and setters", :attribute_name, "attribute value!"
|
12
|
+
it_should_behave_like "proxy with callbacks", :after_build
|
13
|
+
it_should_behave_like "proxy with callbacks", :after_create
|
30
14
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
it "should set the associated instance" do
|
36
|
-
@instance.should have_received.method_missing(:owner=, @association)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should run create when building an association" do
|
41
|
-
association = "associated-instance"
|
42
|
-
associated_factory = "associated-factory"
|
43
|
-
stub(FactoryGirl).factory_by_name { associated_factory }
|
44
|
-
stub(associated_factory).run { association }
|
45
|
-
overrides = { 'attr' => 'value' }
|
46
|
-
@proxy.association(:user, overrides).should == association
|
47
|
-
associated_factory.should have_received.run(FactoryGirl::Proxy::Create, overrides)
|
48
|
-
end
|
49
|
-
|
50
|
-
describe "when asked for the result" do
|
51
|
-
before do
|
52
|
-
@build_spy = Object.new
|
53
|
-
@create_spy = Object.new
|
54
|
-
stub(@build_spy).foo
|
55
|
-
stub(@create_spy).foo
|
56
|
-
@proxy.add_callback(:after_build, proc{ @build_spy.foo })
|
57
|
-
@proxy.add_callback(:after_create, proc{ @create_spy.foo })
|
58
|
-
@result = @proxy.result(nil)
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should save the instance" do
|
62
|
-
@instance.should have_received.save!
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should return the built instance" do
|
66
|
-
@result.should == @instance
|
67
|
-
end
|
68
|
-
|
69
|
-
it "should run both the build and the create callbacks" do
|
70
|
-
@build_spy.should have_received.foo
|
71
|
-
@create_spy.should have_received.foo
|
72
|
-
end
|
15
|
+
it "saves the instance before returning the result" do
|
16
|
+
subject.result(nil)
|
17
|
+
instance.should have_received(:save!)
|
73
18
|
end
|
74
19
|
|
75
20
|
it "runs a custom create block" do
|
76
|
-
block = 'custom create block'
|
77
|
-
|
78
|
-
|
79
|
-
instance
|
80
|
-
block.should have_received.call(instance)
|
21
|
+
block = stub('custom create block', :call => nil)
|
22
|
+
subject.result(block)
|
23
|
+
block.should have_received(:call).with(instance)
|
24
|
+
instance.should have_received(:save!).never
|
81
25
|
end
|
82
26
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
it "should set that value" do
|
89
|
-
@instance.should have_received.method_missing(:attribute=, 'value')
|
90
|
-
end
|
91
|
-
end
|
27
|
+
context "callback execution order" do
|
28
|
+
let(:after_build_callback) { stub("after_build callback", :foo => nil) }
|
29
|
+
let(:after_create_callback) { stub("after_create callback", :foo => nil) }
|
30
|
+
let(:callback_sequence) { sequence("after_* callbacks") }
|
92
31
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
end
|
32
|
+
it "runs after_build callbacks before after_create callbacks" do
|
33
|
+
subject.add_callback(:after_build, proc { after_build_callback.foo })
|
34
|
+
subject.add_callback(:after_create, proc { after_create_callback.foo })
|
97
35
|
|
98
|
-
|
99
|
-
|
100
|
-
end
|
36
|
+
after_build_callback.expects(:foo).once.in_sequence(callback_sequence)
|
37
|
+
after_create_callback.expects(:foo).once.in_sequence(callback_sequence)
|
101
38
|
|
102
|
-
|
103
|
-
@result.should == 'value'
|
39
|
+
subject.result(nil)
|
104
40
|
end
|
105
41
|
end
|
106
42
|
end
|
107
|
-
|
@@ -1,84 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Proxy::Stub do
|
4
|
-
|
5
|
-
|
6
|
-
@instance = "instance"
|
7
|
-
stub(@class).new { @instance }
|
8
|
-
stub(@instance, :id=)
|
9
|
-
stub(@instance).id { 42 }
|
10
|
-
stub(@instance).reload { @instance.connection.reload }
|
4
|
+
let(:instance) { stub("instance", :id= => nil, :id => 42) }
|
5
|
+
let(:proxy_class) { stub("class", :new => instance) }
|
11
6
|
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should not be a new record" do
|
16
|
-
@stub.result(nil).should_not be_new_record
|
17
|
-
end
|
18
|
-
|
19
|
-
it "should be persisted" do
|
20
|
-
@stub.result(nil).should be_persisted
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should not be able to connect to the database" do
|
24
|
-
lambda { @stub.result(nil).reload }.should raise_error(RuntimeError)
|
25
|
-
end
|
26
|
-
|
27
|
-
describe "when a user factory exists" do
|
28
|
-
before do
|
29
|
-
@user = "user"
|
30
|
-
stub(FactoryGirl).factory_by_name { @associated_factory }
|
31
|
-
@associated_factory = 'associate-factory'
|
32
|
-
end
|
33
|
-
|
34
|
-
describe "when asked to associate with another factory" do
|
35
|
-
before do
|
36
|
-
stub(@instance).owner { @user }
|
37
|
-
mock(@associated_factory).run(FactoryGirl::Proxy::Stub, {}) { @user }
|
38
|
-
mock(@stub).set(:owner, @user)
|
39
|
-
|
40
|
-
@stub.associate(:owner, :user, {})
|
41
|
-
end
|
7
|
+
subject { FactoryGirl::Proxy::Stub.new(proxy_class) }
|
42
8
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should return the association when building one" do
|
49
|
-
mock(@associated_factory).run(FactoryGirl::Proxy::Stub, {}) { @user }
|
50
|
-
@stub.association(:user).should == @user
|
51
|
-
end
|
9
|
+
it_should_behave_like "proxy with association support", FactoryGirl::Proxy::Stub
|
10
|
+
it_should_behave_like "proxy with standard getters and setters", :attribute_name, "attribute value!"
|
11
|
+
it_should_behave_like "proxy with callbacks", :after_stub
|
52
12
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
13
|
+
context "asking for a result" do
|
14
|
+
it { subject.result(nil).should_not be_new_record }
|
15
|
+
it { subject.result(nil).should be_persisted }
|
57
16
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
@spy.should have_received.foo
|
17
|
+
[:save, :destroy, :connection, :reload, :update_attribute].each do |database_method|
|
18
|
+
it "raises when attempting to connect to the database by calling #{database_method}" do
|
19
|
+
expect do
|
20
|
+
subject.result(nil).send(database_method)
|
21
|
+
end.to raise_error(RuntimeError, "stubbed models are not allowed to access the database")
|
64
22
|
end
|
65
23
|
end
|
66
24
|
end
|
67
|
-
|
68
|
-
describe "with an existing attribute" do
|
69
|
-
before do
|
70
|
-
@value = "value"
|
71
|
-
mock(@instance).send(:attribute) { @value }
|
72
|
-
mock(@instance).send(:attribute=, @value)
|
73
|
-
@stub.set(:attribute, @value)
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should to the resulting object" do
|
77
|
-
@stub.attribute.should == 'value'
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should return that value when asked for that attribute" do
|
81
|
-
@stub.get(:attribute).should == @value
|
82
|
-
end
|
83
|
-
end
|
84
25
|
end
|
@@ -1,84 +1,84 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Proxy do
|
4
|
-
|
5
|
-
@proxy = FactoryGirl::Proxy.new(Class.new)
|
6
|
-
end
|
4
|
+
subject { FactoryGirl::Proxy.new(Class.new) }
|
7
5
|
|
8
|
-
|
9
|
-
lambda { @proxy.set(:name, 'a name') }.should_not raise_error
|
10
|
-
end
|
6
|
+
it_should_behave_like "proxy without association support"
|
11
7
|
|
12
|
-
it "
|
13
|
-
|
8
|
+
it "doesn't raise when assigning a value to an attribute" do
|
9
|
+
expect { subject.set(:name, "a name") }.to_not raise_error
|
14
10
|
end
|
15
11
|
|
16
|
-
it "
|
17
|
-
|
18
|
-
@proxy.name.should == "it's a name"
|
12
|
+
it "returns nil for an attribute without a value" do
|
13
|
+
subject.get(:name).should be_nil
|
19
14
|
end
|
20
15
|
|
21
|
-
it "
|
22
|
-
|
16
|
+
it "calls get for a missing method" do
|
17
|
+
subject.stubs(:get).with(:name).returns("it's a name")
|
18
|
+
subject.name.should == "it's a name"
|
23
19
|
end
|
24
20
|
|
25
|
-
it "
|
26
|
-
|
21
|
+
it "raises an error when asking for the result" do
|
22
|
+
expect { subject.result(nil) }.to raise_error(NotImplementedError)
|
27
23
|
end
|
28
24
|
|
29
25
|
describe "when adding callbacks" do
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
@proxy.callbacks[:after_create].should be_eql([@first_block])
|
26
|
+
let(:block_1) { proc { "block 1" } }
|
27
|
+
let(:block_2) { proc { "block 2" } }
|
28
|
+
|
29
|
+
it "adds a callback" do
|
30
|
+
subject.add_callback(:after_create, block_1)
|
31
|
+
subject.callbacks[:after_create].should be_eql([block_1])
|
37
32
|
end
|
38
33
|
|
39
|
-
it "
|
40
|
-
|
41
|
-
|
42
|
-
|
34
|
+
it "adds multiple callbacks of the same name" do
|
35
|
+
subject.add_callback(:after_create, block_1)
|
36
|
+
subject.add_callback(:after_create, block_2)
|
37
|
+
subject.callbacks[:after_create].should be_eql([block_1, block_2])
|
43
38
|
end
|
44
39
|
|
45
|
-
it "
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
40
|
+
it "adds multiple callbacks with different names" do
|
41
|
+
subject.add_callback(:after_create, block_1)
|
42
|
+
subject.add_callback(:after_build, block_2)
|
43
|
+
subject.callbacks[:after_create].should be_eql([block_1])
|
44
|
+
subject.callbacks[:after_build].should be_eql([block_2])
|
50
45
|
end
|
51
46
|
end
|
52
47
|
|
53
48
|
describe "when running callbacks" do
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
49
|
+
let(:object_1_within_callback) { stub("call_in_create", :foo => true) }
|
50
|
+
let(:object_2_within_callback) { stub("call_in_create", :foo => true) }
|
51
|
+
|
52
|
+
it "runs all callbacks with a given name" do
|
53
|
+
subject.add_callback(:after_create, proc { object_1_within_callback.foo })
|
54
|
+
subject.add_callback(:after_create, proc { object_2_within_callback.foo })
|
55
|
+
subject.run_callbacks(:after_create)
|
56
|
+
object_1_within_callback.should have_received(:foo).once
|
57
|
+
object_2_within_callback.should have_received(:foo).once
|
59
58
|
end
|
60
59
|
|
61
|
-
it "
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
60
|
+
it "only runs callbacks with a given name" do
|
61
|
+
subject.add_callback(:after_create, proc { object_1_within_callback.foo })
|
62
|
+
subject.add_callback(:after_build, proc { object_2_within_callback.foo })
|
63
|
+
subject.run_callbacks(:after_create)
|
64
|
+
object_1_within_callback.should have_received(:foo).once
|
65
|
+
object_2_within_callback.should have_received(:foo).never
|
67
66
|
end
|
68
67
|
|
69
|
-
it "
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
@second_spy.should_not have_received.foo
|
68
|
+
it "passes in the instance if the block takes an argument" do
|
69
|
+
subject.instance_variable_set("@instance", object_1_within_callback)
|
70
|
+
subject.add_callback(:after_create, proc {|spy| spy.foo })
|
71
|
+
subject.run_callbacks(:after_create)
|
72
|
+
object_1_within_callback.should have_received(:foo).once
|
75
73
|
end
|
76
74
|
|
77
|
-
it "
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
75
|
+
it "passes in the instance and the proxy if the block takes two arguments" do
|
76
|
+
subject.instance_variable_set("@instance", object_1_within_callback)
|
77
|
+
proxy_instance = nil
|
78
|
+
subject.add_callback(:after_create, proc {|spy, proxy| spy.foo; proxy_instance = proxy })
|
79
|
+
subject.run_callbacks(:after_create)
|
80
|
+
object_1_within_callback.should have_received(:foo).once
|
81
|
+
proxy_instance.should == subject
|
82
82
|
end
|
83
83
|
end
|
84
84
|
end
|