forked from thoughtbot/factory_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_strategy_test.rb
91 lines (72 loc) · 2.33 KB
/
create_strategy_test.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
require 'test_helper'
class CreateProxyTest < Test::Unit::TestCase
context "with a class to build" do
setup do
@class = Class.new
@instance = "built-instance"
@association = "associated-instance"
stub(@class).new { @instance }
stub(Factory).create { @association }
stub(@instance).attribute { 'value' }
stub(@instance, :attribute=)
stub(@instance, :owner=)
stub(@instance).save!
end
context "the create proxy" do
setup do
@proxy = Factory::Proxy::Create.new(@class)
end
should "instantiate the class" do
assert_received(@class) {|p| p.new }
end
context "when asked to associate with another factory" do
setup do
@proxy.associate(:owner, :user, {})
end
should "create the associated instance" do
assert_received(Factory) {|p| p.create(:user, {}) }
end
should "set the associated instance" do
assert_received(@instance) {|p| p.method_missing(:owner=, @association) }
end
end
should "call Factory.create when building an association" do
association = 'association'
attribs = { :first_name => 'Billy' }
stub(Factory).create { association }
assert_equal association, @proxy.association(:user, attribs)
assert_received(Factory) {|p| p.create(:user, attribs) }
end
context "when asked for the result" do
setup do
@result = @proxy.result
end
should "save the instance" do
assert_received(@instance) {|p| p.save! }
end
should "return the built instance" do
assert_equal @instance, @result
end
end
context "when setting an attribute" do
setup do
@proxy.set(:attribute, 'value')
end
should "set that value" do
assert_received(@instance) {|p| p.method_missing(:attribute=, 'value') }
end
end
context "when getting an attribute" do
setup do
@result = @proxy.get(:attribute)
end
should "ask the built class for the value" do
assert_received(@instance) {|p| p.attribute }
end
should "return the value for that attribute" do
assert_equal 'value', @result
end
end
end
end
end