much-stub 0.1.0 → 0.1.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.
- checksums.yaml +7 -7
- data/Gemfile +1 -1
- data/README.md +302 -35
- data/lib/much-stub.rb +116 -44
- data/lib/much-stub/call.rb +15 -0
- data/lib/much-stub/call_spy.rb +122 -0
- data/lib/much-stub/version.rb +1 -1
- data/much-stub.gemspec +4 -3
- data/test/helper.rb +3 -3
- data/test/support/factory.rb +1 -2
- data/test/system/much-stub_tests.rb +204 -223
- data/test/unit/call_spy_tests.rb +108 -0
- data/test/unit/call_tests.rb +68 -0
- data/test/unit/much-stub_tests.rb +193 -107
- metadata +41 -38
@@ -0,0 +1,108 @@
|
|
1
|
+
require "assert"
|
2
|
+
require "much-stub/call_spy"
|
3
|
+
|
4
|
+
require "test/support/factory"
|
5
|
+
|
6
|
+
class MuchStub::CallSpy
|
7
|
+
class UnitTests < ::Assert::Context
|
8
|
+
desc "MuchStub::CallSpy"
|
9
|
+
setup do
|
10
|
+
@unit_class = MuchStub::CallSpy
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class InitTests < UnitTests
|
15
|
+
desc "when init"
|
16
|
+
before do
|
17
|
+
@spy = @unit_class.new
|
18
|
+
end
|
19
|
+
subject{ @spy }
|
20
|
+
|
21
|
+
should "spy on method calls and return itself" do
|
22
|
+
assert_true subject.respond_to?(:get)
|
23
|
+
|
24
|
+
assert_equal [], subject.get_calls
|
25
|
+
assert_nil subject.get_last_called_with
|
26
|
+
assert_nil subject.get_called_with
|
27
|
+
assert_equal 0, subject.get_call_count
|
28
|
+
assert_false subject.get_called?
|
29
|
+
|
30
|
+
assert_same subject, subject.get
|
31
|
+
|
32
|
+
assert_true subject.respond_to?(:get)
|
33
|
+
|
34
|
+
assert_kind_of Array, subject.get_calls
|
35
|
+
assert_kind_of MuchStub::Call, subject.get_last_called_with
|
36
|
+
assert_equal [], subject.get_last_called_with.args
|
37
|
+
assert_equal subject.get_last_called_with, subject.get_called_with
|
38
|
+
assert_equal 1, subject.get_call_count
|
39
|
+
assert_true subject.get_called?
|
40
|
+
|
41
|
+
assert_same subject, subject.set!("value1")
|
42
|
+
assert_same subject, subject.set!("value2")
|
43
|
+
|
44
|
+
assert_kind_of Array, subject.set_bang_calls
|
45
|
+
assert_kind_of MuchStub::Call, subject.set_bang_last_called_with
|
46
|
+
assert_equal ["value2"], subject.set_bang_last_called_with.args
|
47
|
+
assert_equal 2, subject.set_bang_call_count
|
48
|
+
assert_true subject.set_bang_called?
|
49
|
+
end
|
50
|
+
|
51
|
+
should "normalize method names in call query methods" do
|
52
|
+
assert_same subject, subject.set!("value1")
|
53
|
+
assert_kind_of Array, subject.set_bang_calls
|
54
|
+
assert_kind_of MuchStub::Call, subject.set_bang_last_called_with
|
55
|
+
assert_equal ["value1"], subject.set_bang_last_called_with.args
|
56
|
+
assert_equal ["value1"], subject.set_bang_called_with.args
|
57
|
+
assert_equal 1, subject.set_bang_call_count
|
58
|
+
assert_true subject.set_bang_called?
|
59
|
+
|
60
|
+
assert_same subject, subject.any?
|
61
|
+
assert_kind_of Array, subject.any_predicate_calls
|
62
|
+
assert_kind_of MuchStub::Call, subject.any_predicate_last_called_with
|
63
|
+
assert_kind_of MuchStub::Call, subject.any_predicate_called_with
|
64
|
+
assert_equal 1, subject.any_predicate_call_count
|
65
|
+
assert_true subject.any_predicate_called?
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class InitWithReturnValuesTests < UnitTests
|
70
|
+
desc "when init with return values"
|
71
|
+
setup do
|
72
|
+
@spy = @unit_class.new(any?: false)
|
73
|
+
end
|
74
|
+
subject{ @spy }
|
75
|
+
|
76
|
+
should "return the given values instead of itself if that method is called" do
|
77
|
+
assert_false subject.get.set!("value1").any?
|
78
|
+
assert_true subject.get_called?
|
79
|
+
assert_equal ["value1"], subject.set_bang_called_with.args
|
80
|
+
assert_true subject.any_predicate_called?
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class InitWithReturnValueProcsTests < UnitTests
|
85
|
+
desc "when init with return value procs"
|
86
|
+
setup do
|
87
|
+
@result = Factory.boolean
|
88
|
+
@spy = @unit_class.new(any?: ->(call) { @result })
|
89
|
+
end
|
90
|
+
subject{ @spy }
|
91
|
+
|
92
|
+
should "return the value of calling the procs instead of itself" do
|
93
|
+
assert_equal @result, subject.get.set!("value1").any?
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
class InitWithNilReturnValuesTests < UnitTests
|
98
|
+
desc "when init with nil return values"
|
99
|
+
setup do
|
100
|
+
@spy = @unit_class.new(result: nil)
|
101
|
+
end
|
102
|
+
subject{ @spy }
|
103
|
+
|
104
|
+
should "return nil" do
|
105
|
+
assert_equal "nil", subject.get.set!("value1").result.inspect
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require "assert"
|
2
|
+
require "much-stub/call"
|
3
|
+
|
4
|
+
require "test/support/factory"
|
5
|
+
|
6
|
+
class MuchStub::Call
|
7
|
+
class UnitTests < Assert::Context
|
8
|
+
desc "MuchStub::Call"
|
9
|
+
setup do
|
10
|
+
@unit_class = MuchStub::Call
|
11
|
+
|
12
|
+
@pargs = [Factory.string, Factory.integer]
|
13
|
+
@kargs = {
|
14
|
+
one: 1,
|
15
|
+
two: 2
|
16
|
+
}
|
17
|
+
@block = -> {}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class InitWithNoArgsTests < UnitTests
|
22
|
+
desc "when init with no args"
|
23
|
+
subject{ @unit_class.new }
|
24
|
+
|
25
|
+
should "know its attrs" do
|
26
|
+
assert_nil subject.pargs
|
27
|
+
assert_nil subject.kargs
|
28
|
+
assert_equal [], subject.args
|
29
|
+
assert_nil subject.block
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class InitWithOnlyPositionalArgsTests < UnitTests
|
34
|
+
desc "when init with only positional args"
|
35
|
+
subject{ @unit_class.new(*@pargs) }
|
36
|
+
|
37
|
+
should "know its attrs" do
|
38
|
+
assert_equal @pargs, subject.pargs
|
39
|
+
assert_nil subject.kargs
|
40
|
+
assert_equal [*@pargs], subject.args
|
41
|
+
assert_nil subject.block
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class InitWithOnlyKeywordArgsTests < UnitTests
|
46
|
+
desc "when init with only keyword args"
|
47
|
+
subject{ @unit_class.new(**@kargs) }
|
48
|
+
|
49
|
+
should "know its attrs" do
|
50
|
+
assert_nil subject.pargs
|
51
|
+
assert_equal @kargs, subject.kargs
|
52
|
+
assert_equal [@kargs], subject.args
|
53
|
+
assert_nil subject.block
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class InitWithBothPositionalAndKeywordArgsTests < UnitTests
|
58
|
+
desc "when init with only keyword args"
|
59
|
+
subject{ @unit_class.new(*@pargs, **@kargs, &@block) }
|
60
|
+
|
61
|
+
should "know its attrs" do
|
62
|
+
assert_equal @pargs, subject.pargs
|
63
|
+
assert_equal @kargs, subject.kargs
|
64
|
+
assert_equal [*@pargs, @kargs], subject.args
|
65
|
+
assert_equal @block, subject.block
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -1,16 +1,15 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "assert"
|
2
|
+
require "much-stub"
|
3
3
|
|
4
|
-
require
|
4
|
+
require "test/support/factory"
|
5
5
|
|
6
6
|
module MuchStub
|
7
|
-
|
8
7
|
class UnitTests < Assert::Context
|
9
8
|
desc "MuchStub"
|
10
9
|
end
|
11
10
|
|
12
|
-
class
|
13
|
-
desc "
|
11
|
+
class APITests < UnitTests
|
12
|
+
desc "API"
|
14
13
|
setup do
|
15
14
|
@orig_value = Factory.string
|
16
15
|
@stub_value = Factory.string
|
@@ -23,20 +22,45 @@ module MuchStub
|
|
23
22
|
end
|
24
23
|
|
25
24
|
should "build a stub" do
|
26
|
-
stub1 = MuchStub.
|
25
|
+
stub1 = MuchStub.(@myobj, :mymeth)
|
27
26
|
assert_kind_of MuchStub::Stub, stub1
|
27
|
+
|
28
|
+
stub2 = MuchStub.stub(@myobj, :mymeth)
|
29
|
+
assert_kind_of MuchStub::Stub, stub2
|
30
|
+
end
|
31
|
+
|
32
|
+
should "build a stub with an on_call block" do
|
33
|
+
my_meth_called_with = nil
|
34
|
+
stub1 =
|
35
|
+
MuchStub.on_call(@myobj, :mymeth) { |call|
|
36
|
+
my_meth_called_with = call
|
37
|
+
}
|
38
|
+
|
39
|
+
@myobj.mymeth
|
40
|
+
assert_kind_of MuchStub::Stub, stub1
|
41
|
+
assert_equal [], my_meth_called_with.args
|
42
|
+
|
43
|
+
my_meth_called_with = nil
|
44
|
+
stub2 =
|
45
|
+
MuchStub.stub_on_call(@myobj, :mymeth) { |call|
|
46
|
+
my_meth_called_with = call
|
47
|
+
}
|
48
|
+
|
49
|
+
@myobj.mymeth
|
50
|
+
assert_kind_of MuchStub::Stub, stub2
|
51
|
+
assert_equal [], my_meth_called_with.args
|
28
52
|
end
|
29
53
|
|
30
54
|
should "lookup stubs that have been called before" do
|
31
|
-
stub1 = MuchStub.
|
32
|
-
stub2 = MuchStub.
|
55
|
+
stub1 = MuchStub.(@myobj, :mymeth)
|
56
|
+
stub2 = MuchStub.(@myobj, :mymeth)
|
33
57
|
assert_same stub1, stub2
|
34
58
|
end
|
35
59
|
|
36
60
|
should "set the stub's do block if given a block" do
|
37
|
-
MuchStub.
|
61
|
+
MuchStub.(@myobj, :mymeth)
|
38
62
|
assert_raises(MuchStub::NotStubbedError){ @myobj.mymeth }
|
39
|
-
MuchStub.
|
63
|
+
MuchStub.(@myobj, :mymeth){ @stub_value }
|
40
64
|
assert_equal @stub_value, @myobj.mymeth
|
41
65
|
end
|
42
66
|
|
@@ -46,7 +70,7 @@ module MuchStub
|
|
46
70
|
assert_equal @orig_value, @myobj.mymeth
|
47
71
|
|
48
72
|
assert_equal @orig_value, @myobj.mymeth
|
49
|
-
MuchStub.
|
73
|
+
MuchStub.(@myobj, :mymeth){ @stub_value }
|
50
74
|
assert_equal @stub_value, @myobj.mymeth
|
51
75
|
MuchStub.unstub(@myobj, :mymeth)
|
52
76
|
assert_equal @orig_value, @myobj.mymeth
|
@@ -55,7 +79,7 @@ module MuchStub
|
|
55
79
|
should "know and teardown all stubs" do
|
56
80
|
assert_equal @orig_value, @myobj.mymeth
|
57
81
|
|
58
|
-
MuchStub.
|
82
|
+
MuchStub.(@myobj, :mymeth){ @stub_value }
|
59
83
|
assert_equal @stub_value, @myobj.mymeth
|
60
84
|
assert_equal 1, MuchStub.stubs.size
|
61
85
|
|
@@ -66,22 +90,76 @@ module MuchStub
|
|
66
90
|
|
67
91
|
should "be able to call a stub's original method" do
|
68
92
|
err = assert_raises(NotStubbedError){ MuchStub.stub_send(@myobj, :mymeth) }
|
69
|
-
assert_includes
|
70
|
-
assert_includes
|
93
|
+
assert_includes "not stubbed.", err.message
|
94
|
+
assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
|
71
95
|
|
72
|
-
MuchStub.
|
96
|
+
MuchStub.(@myobj, :mymeth){ @stub_value }
|
73
97
|
|
74
98
|
assert_equal @stub_value, @myobj.mymeth
|
75
99
|
assert_equal @orig_value, MuchStub.stub_send(@myobj, :mymeth)
|
76
100
|
end
|
77
101
|
|
102
|
+
should "be able to add a stub tap" do
|
103
|
+
my_meth_called_with = nil
|
104
|
+
MuchStub.tap(@myobj, :mymeth){ |value, *args, &block|
|
105
|
+
my_meth_called_with = args
|
106
|
+
}
|
107
|
+
|
108
|
+
assert_equal @orig_value, @myobj.mymeth
|
109
|
+
assert_equal [], my_meth_called_with
|
110
|
+
end
|
111
|
+
|
112
|
+
should "be able to add a stub tap with an on_call block" do
|
113
|
+
my_meth_called_with = nil
|
114
|
+
MuchStub.tap_on_call(@myobj, :mymeth){ |value, call|
|
115
|
+
my_meth_called_with = call
|
116
|
+
}
|
117
|
+
|
118
|
+
assert_equal @orig_value, @myobj.mymeth
|
119
|
+
assert_equal [], my_meth_called_with.args
|
120
|
+
end
|
121
|
+
|
122
|
+
should "be able to add a stubbed spy" do
|
123
|
+
myclass = Class.new do
|
124
|
+
def one; self; end
|
125
|
+
def two(val); self; end
|
126
|
+
def three; self; end
|
127
|
+
def ready?; false; end
|
128
|
+
end
|
129
|
+
myobj = myclass.new
|
130
|
+
|
131
|
+
spy =
|
132
|
+
MuchStub.spy(
|
133
|
+
myobj,
|
134
|
+
:one,
|
135
|
+
:two,
|
136
|
+
:three,
|
137
|
+
:to_s,
|
138
|
+
ready?: true)
|
139
|
+
|
140
|
+
assert_equal spy, myobj.one
|
141
|
+
assert_equal spy, myobj.two("a")
|
142
|
+
assert_equal spy, myobj.three
|
143
|
+
assert_equal spy, myobj.to_s
|
144
|
+
|
145
|
+
assert_true myobj.one.two("b").three.ready?
|
146
|
+
|
147
|
+
assert_kind_of MuchStub::CallSpy, spy
|
148
|
+
assert_equal 2, spy.one_call_count
|
149
|
+
assert_equal 2, spy.two_call_count
|
150
|
+
assert_equal 2, spy.three_call_count
|
151
|
+
assert_equal 1, spy.to_s_call_count
|
152
|
+
assert_equal 1, spy.ready_predicate_call_count
|
153
|
+
assert_equal ["b"], spy.two_last_called_with.args
|
154
|
+
assert_true spy.ready_predicate_called?
|
155
|
+
end
|
78
156
|
end
|
79
157
|
|
80
158
|
class StubTests < UnitTests
|
81
159
|
desc "Stub"
|
82
160
|
setup do
|
83
161
|
@myclass = Class.new do
|
84
|
-
def mymeth;
|
162
|
+
def mymeth; "meth"; end
|
85
163
|
def myval(val); val; end
|
86
164
|
def myargs(*args); args; end
|
87
165
|
def myvalargs(val1, val2, *args); [val1, val2, args]; end
|
@@ -105,7 +183,7 @@ module MuchStub
|
|
105
183
|
end
|
106
184
|
|
107
185
|
should "know its names" do
|
108
|
-
assert_equal
|
186
|
+
assert_equal "mymeth", subject.method_name
|
109
187
|
expected = "__muchstub_stub__#{@myobj.object_id}_#{subject.method_name}"
|
110
188
|
assert_equal expected, subject.name
|
111
189
|
expected = "@__muchstub_stub_#{@myobj.object_id}_" \
|
@@ -115,44 +193,49 @@ module MuchStub
|
|
115
193
|
|
116
194
|
should "complain when called if no do block was given" do
|
117
195
|
err = assert_raises(MuchStub::NotStubbedError){ @myobj.mymeth }
|
118
|
-
assert_includes
|
119
|
-
assert_includes
|
196
|
+
assert_includes "not stubbed.", err.message
|
197
|
+
assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
|
120
198
|
|
121
|
-
subject.do = proc{
|
199
|
+
subject.do = proc{ "mymeth" }
|
122
200
|
assert_nothing_raised do
|
123
201
|
@myobj.mymeth
|
124
202
|
end
|
125
203
|
|
204
|
+
MuchStub::Stub.new(@myobj, :mymeth){ "mymeth" }
|
126
205
|
assert_nothing_raised do
|
127
|
-
|
206
|
+
@myobj.mymeth
|
128
207
|
end
|
129
208
|
end
|
130
209
|
|
131
|
-
should "complain
|
132
|
-
|
133
|
-
|
134
|
-
|
210
|
+
should "complain when called if no lookup block was given" do
|
211
|
+
MuchStub::Stub.new(@myobj, :myval).with(1)
|
212
|
+
|
213
|
+
err = assert_raises(MuchStub::StubError){ @myobj.myval(1) }
|
214
|
+
assert_includes "stubbed with no block.", err.message
|
215
|
+
assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
|
135
216
|
end
|
136
217
|
|
137
|
-
should "complain if
|
138
|
-
assert_raises(MuchStub::
|
218
|
+
should "complain if stubbing a method that the object doesn't respond to" do
|
219
|
+
err = assert_raises(MuchStub::StubError){ MuchStub::Stub.new(@myobj, :some_other_meth) }
|
220
|
+
assert_includes "does not respond to", err.message
|
221
|
+
assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
|
139
222
|
end
|
140
223
|
|
141
224
|
should "complain if stubbed and called with mismatched arity" do
|
142
|
-
MuchStub::Stub.new(@myobj, :myval){
|
225
|
+
MuchStub::Stub.new(@myobj, :myval){ "myval" }
|
143
226
|
err = assert_raises(MuchStub::StubArityError){ @myobj.myval }
|
144
|
-
assert_includes
|
145
|
-
assert_includes
|
227
|
+
assert_includes "arity mismatch on", err.message
|
228
|
+
assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
|
146
229
|
|
147
230
|
assert_nothing_raised { @myobj.myval(1) }
|
148
231
|
assert_raises(MuchStub::StubArityError){ @myobj.myval(1,2) }
|
149
232
|
|
150
|
-
MuchStub::Stub.new(@myobj, :myargs){
|
233
|
+
MuchStub::Stub.new(@myobj, :myargs){ "myargs" }
|
151
234
|
assert_nothing_raised { @myobj.myargs }
|
152
235
|
assert_nothing_raised { @myobj.myargs(1) }
|
153
236
|
assert_nothing_raised { @myobj.myargs(1,2) }
|
154
237
|
|
155
|
-
MuchStub::Stub.new(@myobj, :myvalargs){
|
238
|
+
MuchStub::Stub.new(@myobj, :myvalargs){ "myvalargs" }
|
156
239
|
assert_raises(MuchStub::StubArityError){ @myobj.myvalargs }
|
157
240
|
assert_raises(MuchStub::StubArityError){ @myobj.myvalargs(1) }
|
158
241
|
assert_nothing_raised { @myobj.myvalargs(1,2) }
|
@@ -161,75 +244,84 @@ module MuchStub
|
|
161
244
|
|
162
245
|
should "complain if stubbed with mismatched arity" do
|
163
246
|
err = assert_raises(MuchStub::StubArityError) do
|
164
|
-
MuchStub::Stub.new(@myobj, :myval).with(){
|
247
|
+
MuchStub::Stub.new(@myobj, :myval).with(){ "myval" }
|
165
248
|
end
|
166
|
-
assert_includes
|
167
|
-
assert_includes
|
249
|
+
assert_includes "arity mismatch on", err.message
|
250
|
+
assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
|
168
251
|
|
169
252
|
assert_raises(MuchStub::StubArityError) do
|
170
|
-
MuchStub::Stub.new(@myobj, :myval).with(1,2){
|
253
|
+
MuchStub::Stub.new(@myobj, :myval).with(1,2){ "myval" }
|
171
254
|
end
|
172
255
|
assert_nothing_raised do
|
173
|
-
MuchStub::Stub.new(@myobj, :myval).with(1){
|
256
|
+
MuchStub::Stub.new(@myobj, :myval).with(1){ "myval" }
|
174
257
|
end
|
175
258
|
|
176
259
|
assert_nothing_raised do
|
177
|
-
MuchStub::Stub.new(@myobj, :myargs).with(){
|
260
|
+
MuchStub::Stub.new(@myobj, :myargs).with(){ "myargs" }
|
178
261
|
end
|
179
262
|
assert_nothing_raised do
|
180
|
-
MuchStub::Stub.new(@myobj, :myargs).with(1,2){
|
263
|
+
MuchStub::Stub.new(@myobj, :myargs).with(1,2){ "myargs" }
|
181
264
|
end
|
182
265
|
assert_nothing_raised do
|
183
|
-
MuchStub::Stub.new(@myobj, :myargs).with(1){
|
266
|
+
MuchStub::Stub.new(@myobj, :myargs).with(1){ "myargs" }
|
184
267
|
end
|
185
268
|
|
186
269
|
assert_raises(MuchStub::StubArityError) do
|
187
|
-
MuchStub::Stub.new(@myobj, :myvalargs).with(){
|
270
|
+
MuchStub::Stub.new(@myobj, :myvalargs).with(){ "myvalargs" }
|
188
271
|
end
|
189
272
|
assert_raises(MuchStub::StubArityError) do
|
190
|
-
MuchStub::Stub.new(@myobj, :myvalargs).with(1){
|
273
|
+
MuchStub::Stub.new(@myobj, :myvalargs).with(1){ "myvalargs" }
|
191
274
|
end
|
192
275
|
assert_nothing_raised do
|
193
|
-
MuchStub::Stub.new(@myobj, :myvalargs).with(1,2){
|
276
|
+
MuchStub::Stub.new(@myobj, :myvalargs).with(1,2){ "myvalargs" }
|
194
277
|
end
|
195
278
|
assert_nothing_raised do
|
196
|
-
MuchStub::Stub.new(@myobj, :myvalargs).with(1,2,3){
|
279
|
+
MuchStub::Stub.new(@myobj, :myvalargs).with(1,2,3){ "myvalargs" }
|
197
280
|
end
|
198
281
|
end
|
199
282
|
|
200
283
|
should "stub methods with no args" do
|
201
284
|
subject.teardown
|
202
285
|
|
203
|
-
assert_equal
|
204
|
-
MuchStub::Stub.new(@myobj, :mymeth){
|
205
|
-
assert_equal
|
286
|
+
assert_equal "meth", @myobj.mymeth
|
287
|
+
MuchStub::Stub.new(@myobj, :mymeth){ "mymeth" }
|
288
|
+
assert_equal "mymeth", @myobj.mymeth
|
206
289
|
end
|
207
290
|
|
208
291
|
should "stub methods with required arg" do
|
209
292
|
assert_equal 1, @myobj.myval(1)
|
210
293
|
stub = MuchStub::Stub.new(@myobj, :myval){ |val| val.to_s }
|
211
|
-
assert_equal
|
212
|
-
assert_equal
|
213
|
-
stub.with(2){
|
214
|
-
assert_equal
|
294
|
+
assert_equal "1", @myobj.myval(1)
|
295
|
+
assert_equal "2", @myobj.myval(2)
|
296
|
+
stub.with(2){ "two" }
|
297
|
+
assert_equal "two", @myobj.myval(2)
|
215
298
|
end
|
216
299
|
|
217
300
|
should "stub methods with variable args" do
|
218
301
|
assert_equal [1,2], @myobj.myargs(1,2)
|
219
|
-
|
220
|
-
|
221
|
-
assert_equal
|
222
|
-
stub.with(3,4,5){
|
223
|
-
assert_equal
|
302
|
+
|
303
|
+
stub = MuchStub::Stub.new(@myobj, :myargs){ |*args| args.join(",") }
|
304
|
+
assert_equal "1,2,3", @myobj.myargs(1,2,3)
|
305
|
+
stub.with(3,4,5){ |*args| args.join(":") }
|
306
|
+
assert_equal "3:4:5", @myobj.myargs(3,4,5)
|
307
|
+
|
308
|
+
stub = MuchStub::Stub.new(@myobj, :myargs).on_call{ |call| call.args.join(",") }
|
309
|
+
assert_equal "1,2,3", @myobj.myargs(1,2,3)
|
310
|
+
stub.with(3,4,5).on_call{ |call| call.args.join(":") }
|
311
|
+
assert_equal "3:4:5", @myobj.myargs(3,4,5)
|
224
312
|
end
|
225
313
|
|
226
314
|
should "stub methods with required args and variable args" do
|
227
315
|
assert_equal [1,2, [3]], @myobj.myvalargs(1,2,3)
|
228
|
-
stub = MuchStub::Stub.new(@myobj, :myvalargs){ |*args| args.join(
|
229
|
-
assert_equal
|
230
|
-
|
231
|
-
|
232
|
-
|
316
|
+
stub = MuchStub::Stub.new(@myobj, :myvalargs){ |*args| args.join(",") }
|
317
|
+
assert_equal "1,2,3", @myobj.myvalargs(1,2,3)
|
318
|
+
stub.with(3,4,5){ |*args| args.join(":") }
|
319
|
+
assert_equal "3:4:5", @myobj.myvalargs(3,4,5)
|
320
|
+
|
321
|
+
stub = MuchStub::Stub.new(@myobj, :myvalargs).on_call{ |call| call.args.join(",") }
|
322
|
+
assert_equal "1,2,3", @myobj.myvalargs(1,2,3)
|
323
|
+
stub.with(3,4,5).on_call{ |call| call.args.join(":") }
|
324
|
+
assert_equal "3:4:5", @myobj.myvalargs(3,4,5)
|
233
325
|
end
|
234
326
|
|
235
327
|
should "stub methods that yield blocks" do
|
@@ -239,9 +331,9 @@ module MuchStub
|
|
239
331
|
assert_equal true, blkcalled
|
240
332
|
|
241
333
|
blkcalled = false
|
242
|
-
MuchStub::Stub.new(@myobj, :myblk){ blkcalled =
|
334
|
+
MuchStub::Stub.new(@myobj, :myblk){ blkcalled = "true" }
|
243
335
|
@myobj.myblk(&blk)
|
244
|
-
assert_equal
|
336
|
+
assert_equal "true", blkcalled
|
245
337
|
end
|
246
338
|
|
247
339
|
should "stub methods even if they are not local to the object" do
|
@@ -260,61 +352,61 @@ module MuchStub
|
|
260
352
|
|
261
353
|
assert_equal [1,2,[3]], mydelegator.myvalargs(1,2,3)
|
262
354
|
stub = MuchStub::Stub.new(mydelegator, :myvalargs){ |*args| args.inspect }
|
263
|
-
assert_equal
|
264
|
-
assert_equal
|
265
|
-
stub.with(4,5,6){
|
266
|
-
assert_equal
|
355
|
+
assert_equal "[1, 2, 3]", mydelegator.myvalargs(1,2,3)
|
356
|
+
assert_equal "[4, 5, 6]", mydelegator.myvalargs(4,5,6)
|
357
|
+
stub.with(4,5,6){ "four-five-six" }
|
358
|
+
assert_equal "four-five-six", mydelegator.myvalargs(4,5,6)
|
267
359
|
end
|
268
360
|
|
269
361
|
should "call to the original method" do
|
270
362
|
subject.teardown
|
271
363
|
|
272
364
|
# no args
|
273
|
-
stub = MuchStub::Stub.new(@myobj, :mymeth){
|
274
|
-
assert_equal
|
275
|
-
assert_equal
|
365
|
+
stub = MuchStub::Stub.new(@myobj, :mymeth){ "mymeth" }
|
366
|
+
assert_equal "mymeth", stub.call([])
|
367
|
+
assert_equal "meth", stub.call_method([])
|
276
368
|
|
277
369
|
# static args
|
278
370
|
stub = MuchStub::Stub.new(@myobj, :myval){ |val| val.to_s }
|
279
|
-
assert_equal
|
371
|
+
assert_equal "1", stub.call([1])
|
280
372
|
assert_equal 1, stub.call_method([1])
|
281
|
-
assert_equal
|
373
|
+
assert_equal "2", stub.call([2])
|
282
374
|
assert_equal 2, stub.call_method([2])
|
283
|
-
stub.with(2){
|
284
|
-
assert_equal
|
375
|
+
stub.with(2){ "two" }
|
376
|
+
assert_equal "two", stub.call([2])
|
285
377
|
assert_equal 2, stub.call_method([2])
|
286
378
|
|
287
379
|
# dynamic args
|
288
|
-
stub = MuchStub::Stub.new(@myobj, :myargs){ |*args| args.join(
|
289
|
-
assert_equal
|
380
|
+
stub = MuchStub::Stub.new(@myobj, :myargs){ |*args| args.join(",") }
|
381
|
+
assert_equal "1,2", stub.call([1,2])
|
290
382
|
assert_equal [1,2], stub.call_method([1,2])
|
291
|
-
assert_equal
|
383
|
+
assert_equal "3,4,5", stub.call([3,4,5])
|
292
384
|
assert_equal [3,4,5], stub.call_method([3,4,5])
|
293
|
-
stub.with(3,4,5){
|
294
|
-
assert_equal
|
385
|
+
stub.with(3,4,5){ "three-four-five" }
|
386
|
+
assert_equal "three-four-five", stub.call([3,4,5])
|
295
387
|
assert_equal [3,4,5], stub.call_method([3,4,5])
|
296
388
|
|
297
389
|
# mixed static/dynamic args
|
298
|
-
stub = MuchStub::Stub.new(@myobj, :myvalargs){ |*args| args.join(
|
299
|
-
assert_equal
|
390
|
+
stub = MuchStub::Stub.new(@myobj, :myvalargs){ |*args| args.join(",") }
|
391
|
+
assert_equal "1,2,3", stub.call([1,2,3])
|
300
392
|
assert_equal [1,2, [3]], stub.call_method([1,2,3])
|
301
|
-
assert_equal
|
393
|
+
assert_equal "3,4,5", stub.call([3,4,5])
|
302
394
|
assert_equal [3,4,[5]], stub.call_method([3,4,5])
|
303
|
-
stub.with(3,4,5){
|
304
|
-
assert_equal
|
395
|
+
stub.with(3,4,5){ "three-four-five" }
|
396
|
+
assert_equal "three-four-five", stub.call([3,4,5])
|
305
397
|
assert_equal [3,4,[5]], stub.call_method([3,4,5])
|
306
398
|
|
307
399
|
# blocks
|
308
400
|
blkcalled = false
|
309
401
|
blk = proc{ blkcalled = true }
|
310
|
-
stub = MuchStub::Stub.new(@myobj, :myblk){ blkcalled =
|
402
|
+
stub = MuchStub::Stub.new(@myobj, :myblk){ blkcalled = "true" }
|
311
403
|
stub.call([], &blk)
|
312
|
-
assert_equal
|
404
|
+
assert_equal "true", blkcalled
|
313
405
|
stub.call_method([], &blk)
|
314
406
|
assert_equal true, blkcalled
|
315
407
|
end
|
316
408
|
|
317
|
-
should "store and remove itself
|
409
|
+
should "store and remove itself" do
|
318
410
|
assert_equal subject, MuchStub.instance_variable_get(subject.ivar_name)
|
319
411
|
subject.teardown
|
320
412
|
assert_nil MuchStub.instance_variable_get(subject.ivar_name)
|
@@ -323,17 +415,16 @@ module MuchStub
|
|
323
415
|
should "be removable" do
|
324
416
|
assert_equal 1, @myobj.myval(1)
|
325
417
|
stub = MuchStub::Stub.new(@myobj, :myval){ |val| val.to_s }
|
326
|
-
assert_equal
|
418
|
+
assert_equal "1", @myobj.myval(1)
|
327
419
|
stub.teardown
|
328
420
|
assert_equal 1, @myobj.myval(1)
|
329
421
|
end
|
330
422
|
|
331
423
|
should "have a readable inspect" do
|
332
|
-
expected = "#<#{subject.class}:#{
|
424
|
+
expected = "#<#{subject.class}:#{"0x0%x" % (subject.object_id << 1)}" \
|
333
425
|
" @method_name=#{subject.method_name.inspect}>"
|
334
426
|
assert_equal expected, subject.inspect
|
335
427
|
end
|
336
|
-
|
337
428
|
end
|
338
429
|
|
339
430
|
class MutatingArgsStubTests < StubTests
|
@@ -351,7 +442,6 @@ module MuchStub
|
|
351
442
|
should "not raise a stub error when called" do
|
352
443
|
assert_nothing_raised{ @stub.call([@arg]) }
|
353
444
|
end
|
354
|
-
|
355
445
|
end
|
356
446
|
|
357
447
|
class StubInheritedMethodAfterStubbedOnParentTests < StubTests
|
@@ -366,11 +456,11 @@ module MuchStub
|
|
366
456
|
|
367
457
|
should "allow stubbing them independently of each other" do
|
368
458
|
assert_nothing_raised do
|
369
|
-
@parent_stub.with(1, 2){
|
370
|
-
@child_stub.with(1, 2){
|
459
|
+
@parent_stub.with(1, 2){ "parent" }
|
460
|
+
@child_stub.with(1, 2){ "child" }
|
371
461
|
end
|
372
|
-
assert_equal
|
373
|
-
assert_equal
|
462
|
+
assert_equal "parent", @parent_class.new(1, 2)
|
463
|
+
assert_equal "child", @child_class.new(1, 2)
|
374
464
|
end
|
375
465
|
|
376
466
|
should "not raise any errors when tearing down the parent before the child" do
|
@@ -379,7 +469,6 @@ module MuchStub
|
|
379
469
|
@child_stub.teardown
|
380
470
|
end
|
381
471
|
end
|
382
|
-
|
383
472
|
end
|
384
473
|
|
385
474
|
class NullStubTests < UnitTests
|
@@ -390,7 +479,6 @@ module MuchStub
|
|
390
479
|
subject{ @ns }
|
391
480
|
|
392
481
|
should have_imeths :teardown
|
393
|
-
|
394
482
|
end
|
395
483
|
|
396
484
|
class ParameterListTests < UnitTests
|
@@ -398,7 +486,7 @@ module MuchStub
|
|
398
486
|
setup do
|
399
487
|
many_args = (0..ParameterList::LETTERS.size).map do
|
400
488
|
Assert::Factory.string
|
401
|
-
end.join(
|
489
|
+
end.join(", ")
|
402
490
|
@object = Class.new
|
403
491
|
# use `class_eval` with string to easily define methods with many params
|
404
492
|
@object.class_eval <<-methods
|
@@ -411,23 +499,22 @@ module MuchStub
|
|
411
499
|
subject{ ParameterList }
|
412
500
|
|
413
501
|
should "build a parameter list for a method that takes no args" do
|
414
|
-
assert_equal
|
502
|
+
assert_equal "&block", subject.new(@object, "noargs")
|
415
503
|
end
|
416
504
|
|
417
505
|
should "build a parameter list for a method that takes any args" do
|
418
|
-
assert_equal
|
506
|
+
assert_equal "*args, &block", subject.new(@object, "anyargs")
|
419
507
|
end
|
420
508
|
|
421
509
|
should "build a parameter list for a method that takes many args" do
|
422
|
-
expected = "#{ParameterList::LETTERS.join(
|
423
|
-
assert_equal expected, subject.new(@object,
|
510
|
+
expected = "#{ParameterList::LETTERS.join(", ")}, aa, &block"
|
511
|
+
assert_equal expected, subject.new(@object, "manyargs")
|
424
512
|
end
|
425
513
|
|
426
514
|
should "build a parameter list for a method that takes a minimum number of args" do
|
427
|
-
expected = "#{ParameterList::LETTERS.join(
|
428
|
-
assert_equal expected, subject.new(@object,
|
515
|
+
expected = "#{ParameterList::LETTERS.join(", ")}, aa, *args, &block"
|
516
|
+
assert_equal expected, subject.new(@object, "minargs")
|
429
517
|
end
|
430
|
-
|
431
518
|
end
|
432
519
|
|
433
520
|
class ChangeHashObject
|
@@ -443,5 +530,4 @@ module MuchStub
|
|
443
530
|
@value = 1
|
444
531
|
end
|
445
532
|
end
|
446
|
-
|
447
533
|
end
|