This repository has been archived by the owner on Jan 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_test.rb
121 lines (98 loc) · 3.2 KB
/
app_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
# frozen_string_literal: true
require "debug"
require "minitest/autorun"
require_relative "app"
require_relative "spy_helper"
class CarTest < Minitest::Test
def setup
@car = Car.new("Honda")
end
def test_simple
@car1 = Car.new("Suzuki")
@car2 = Car.new("Toyota")
calls =
SpyHelper.spy_on(@car1, method: :honk) do
@car1.honk(5)
@car2.honk(3)
end
assert_equal 1, calls.length
call = calls.first
assert_equal(@car1, call.object)
assert_equal(:honk, call.method_name)
assert_equal([5], call.args)
assert_equal({}, call.kwargs)
assert_equal(%i[beep beep beep beep beep], call.return_value)
end
def test_kwargs
calls = SpyHelper.spy_on(@car, method: :honk) { @car.honk(2, sound: :boop) }
assert_equal 1, calls.length
call = calls.first
assert_equal(@car, call.object)
assert_equal(:honk, call.method_name)
assert_equal([2], call.args)
assert_equal({ sound: :boop }, call.kwargs)
assert_equal %i[boop boop], call.return_value
end
def test_multiple_calls
calls =
SpyHelper.spy_on(@car, method: :honk) do
@car.honk(3)
@car.honk(4)
end
assert_equal 2, calls.length
first_call = calls.first
second_call = calls.last
assert_equal(@car, first_call.object)
assert_equal(:honk, first_call.method_name)
assert_equal([3], first_call.args)
assert_equal({}, first_call.kwargs)
assert_equal(%i[beep beep beep], first_call.return_value)
assert_equal(@car, second_call.object)
assert_equal(:honk, second_call.method_name)
assert_equal([4], second_call.args)
assert_equal({}, second_call.kwargs)
assert_equal(%i[beep beep beep beep], second_call.return_value)
end
def test_multiple_methods
calls =
SpyHelper.spy_on(@car, methods: %i[honk drive]) do
@car.honk(5)
@car.drive
end
assert_equal 2, calls.length
first_call = calls.first
second_call = calls.last
assert_equal(@car, first_call.object)
assert_equal(:honk, first_call.method_name)
assert_equal([5], first_call.args)
assert_equal({}, first_call.kwargs)
assert_equal(%i[beep beep beep beep beep], first_call.return_value)
assert_equal(@car, second_call.object)
assert_equal(:drive, second_call.method_name)
assert_equal([], second_call.args)
assert_equal({}, second_call.kwargs)
assert_equal(:drive, second_call.return_value)
end
def test_any_instance
@car1 = Car.new("Suzuki")
@car2 = Car.new("Toyota")
calls =
SpyHelper.spy_on(Car, method: :honk, any_instance: true) do
@car1.honk(5)
@car2.honk(3)
end
assert_equal 2, calls.length
first_call = calls.first
second_call = calls.last
assert_equal(@car1, first_call.object)
assert_equal(:honk, first_call.method_name)
assert_equal([5], first_call.args)
assert_equal({}, first_call.kwargs)
assert_equal(%i[beep beep beep beep beep], first_call.return_value)
assert_equal(@car2, second_call.object)
assert_equal(:honk, second_call.method_name)
assert_equal([3], second_call.args)
assert_equal({}, second_call.kwargs)
assert_equal(%i[beep beep beep], second_call.return_value)
end
end