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