forked from thoughtbot/factory_bot
/
create_spec.rb
107 lines (88 loc) · 2.94 KB
/
create_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require 'spec_helper'
describe FactoryGirl::Proxy::Create do
before do
@class = Class.new
@instance = "built-instance"
stub(@class).new { @instance }
stub(@instance).attribute { 'value' }
stub(@instance, :attribute=)
stub(@instance, :owner=)
stub(@instance).save!
@proxy = FactoryGirl::Proxy::Create.new(@class)
end
it "should instantiate the class" do
@class.should have_received.new
end
describe "when asked to associate with another factory" do
before do
@association = "associated-instance"
@associated_factory = "associated-factory"
stub(FactoryGirl).factory_by_name { @associated_factory }
stub(@associated_factory).run { @association }
@overrides = { 'attr' => 'value' }
@proxy.associate(:owner, :user, @overrides)
end
it "should create the associated instance" do
@associated_factory.should have_received.run(FactoryGirl::Proxy::Create, @overrides)
end
it "should set the associated instance" do
@instance.should have_received.method_missing(:owner=, @association)
end
end
it "should run create when building an association" do
association = "associated-instance"
associated_factory = "associated-factory"
stub(FactoryGirl).factory_by_name { associated_factory }
stub(associated_factory).run { association }
overrides = { 'attr' => 'value' }
@proxy.association(:user, overrides).should == association
associated_factory.should have_received.run(FactoryGirl::Proxy::Create, overrides)
end
describe "when asked for the result" do
before do
@build_spy = Object.new
@create_spy = Object.new
stub(@build_spy).foo
stub(@create_spy).foo
@proxy.add_callback(:after_build, proc{ @build_spy.foo })
@proxy.add_callback(:after_create, proc{ @create_spy.foo })
@result = @proxy.result(nil)
end
it "should save the instance" do
@instance.should have_received.save!
end
it "should return the built instance" do
@result.should == @instance
end
it "should run both the build and the create callbacks" do
@build_spy.should have_received.foo
@create_spy.should have_received.foo
end
end
it "runs a custom create block" do
block = 'custom create block'
stub(block).call
stub(@instance).save! { raise }
instance = @proxy.result(block)
block.should have_received.call(instance)
end
describe "when setting an attribute" do
before do
@proxy.set(:attribute, 'value')
end
it "should set that value" do
@instance.should have_received.method_missing(:attribute=, 'value')
end
end
describe "when getting an attribute" do
before do
@result = @proxy.get(:attribute)
end
it "should ask the built class for the value" do
@instance.should have_received.attribute
end
it "should return the value for that attribute" do
@result.should == 'value'
end
end
end