-
Notifications
You must be signed in to change notification settings - Fork 7
/
definition_test.rb
137 lines (115 loc) · 4.97 KB
/
definition_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true
require "test_helper"
module FixtureFactory
class DefinitionTest < FixtureFactory::TestCase
setup do
@tested_class = FixtureFactory::Definition
@empty_block = tested_class::EMPTY_BLOCK
end
attr_reader :tested_class, :empty_block
test "#initialize derives from default parent defaults" do
subject = tested_class.new(:string)
assert_instance_of tested_class, subject.parent
assert_equal String, subject.klass
assert_equal "strings", subject.fixture_method
assert_nil subject.fixture_name
assert_same empty_block, subject.instance_variable_get(:@block)
end
test "#initialize allows explicit parent" do
parent = tested_class.new(:parent_fixtury, class: -> { Object }, via: :parent_method, like: :parent_name)
subject = tested_class.new(:child_fixtury, parent: parent)
assert_same parent, subject.parent
assert_equal parent.klass, subject.klass
assert_equal parent.fixture_method, subject.fixture_method
assert_equal parent.fixture_name, subject.fixture_name
end
test "#initialize allows explicit class" do
subject = tested_class.new(:child_fixtury, class: -> { String })
assert_equal String, subject.klass
end
test "#initialize allows explicit fixture_method" do
subject = tested_class.new(:child_fixtury, via: :some_method)
assert_equal :some_method, subject.fixture_method
end
test "#initialize allows explicit fixture_name" do
subject = tested_class.new(:child_fixtury, like: :some_name)
assert_equal :some_name, subject.fixture_name
end
test "#initialize allows explicit block" do
block = proc {}
subject = tested_class.new(:child_fixtury, block: block)
assert_same block, subject.instance_variable_get(:@block)
end
test "#block wraps block ivar and parent block in a hash merge reducer" do
parent_block = proc { { foo: :bar, fizz: :buzz } }
child_block = proc { { foo: :baz, great: :scott } }
subject = tested_class.new(:fixture, block: child_block)
subject.parent.block = parent_block
assert_equal({ foo: :baz, fizz: :buzz, great: :scott }, subject.block.call)
end
test "#block wraps all blocks in provided context" do
parent_block = proc { parent_attributes }
child_block = proc { child_attributes }
subject = tested_class.new(:fixture, block: child_block)
subject.parent.block = parent_block
assert_equal({ parent: true, child: true }, FixtureFactory.evaluate(subject.block, context: self))
end
test "#klass= assigns classes normally" do
subject = tested_class.new(:fixture)
subject.proc_class = -> { Object }
assert_equal Object, subject.klass
end
test "#klass raises WrongClassError when class is invalid" do
subject = tested_class.new(:fixture)
subject.proc_class = -> { TheClassIsALie }
error = assert_raises(WrongClassError) do
subject.klass
end
assert_equal <<~MSG.squish, error.message
Constant defined in file #{__FILE__} on line 77 is not defined.
Try using the `class` option in your definition to specify a valid class name.
https://github.com/Shopify/fixture_factory/blob/master/README.md#naming
MSG
end
test "#fixture_args returns fixture arguments" do
subject = tested_class.new(:fixture, via: :users, like: :bob)
assert_equal [:users, :bob], subject.fixture_args
end
test "#from_fixture? returns true when fixture attributes present" do
subject = tested_class.new(:fixture)
refute_predicate subject, :from_fixture?
end
test "#from_fixture? returns false when fixture attributes blank" do
subject = tested_class.new(:fixture, via: :meth, like: :name)
assert_predicate subject, :from_fixture?
end
test "#run evaluates fixtureless fixture in a given context" do
block = proc { child_attributes }
subject = tested_class.new(:user, block: block)
assert_equal({ child: true }, subject.run(context: self))
end
test "#run evaluates fixture fixture in a given context" do
block = proc { child_attributes }
subject = tested_class.new(:user, like: :bob, block: block)
assert_equal({ child: true, name: "Bob" }, subject.run(context: self).slice(:child, :name))
end
test "#run raises WrongFixtureMethodError when fixture method is invalid" do
subject = tested_class.new(:user, via: :cool_users, like: :bob)
error = assert_raises(WrongFixtureMethodError) do
subject.run(context: self)
end
assert_equal <<~MSG.squish, error.message
No fixture method named "cool_users".
Try using the `via` option in your definition to specify a valid method.
https://github.com/Shopify/fixture_factory/blob/master/README.md#naming
MSG
end
private
def parent_attributes
{ parent: true, child: false }
end
def child_attributes
{ child: true }
end
end
end