-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmocking_methodful_classes_test.rb
100 lines (79 loc) · 2.68 KB
/
mocking_methodful_classes_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
# typed: strict
require "test_helper"
class MockingMethodfulClassesTest < Minitest::Test
include Mocktail::DSL
extend T::Sig
class OverridesEquals
extend T::Sig
sig { returns(T.untyped) }
def normal_method
end
sig { params(other: T.untyped).returns(T.untyped) }
def ==(other)
super
end
end
sig { void }
def test_overriding_equals
overrides_equals = Mocktail.of(OverridesEquals)
overrides_equals.normal_method
stubs { overrides_equals.normal_method }.with { 32 }
assert_equal overrides_equals.normal_method, 32
end
class OverridesEverything
extend T::Sig
# puts instance_methods.map { |m| ":#{m}," }.join("\n")
(instance_methods - [:__send__, :object_id, :nil?, :is_a?]).each do |method|
define_method method, ->(*args, **kwargs, &block) {
T.unsafe(self).super
}
end
sig { params(arg: T.untyped).returns(T.untyped) }
def normal_method(arg = nil)
end
end
sig { void }
def test_overriding_everything
overrides_everything = Mocktail.of(OverridesEverything)
overrides_everything.normal_method
stubs { overrides_everything.normal_method }.with { 32 }
stubs { |m| overrides_everything == m.any }.with { false }
stubs { overrides_everything == 3 }.with { true }
assert_equal overrides_everything.normal_method, 32
assert_equal overrides_everything == 3, true
assert_equal overrides_everything == 2, false
verify { overrides_everything.normal_method }
verify { overrides_everything == 3 }
verify(times: 0) { overrides_everything == 4 }
explanation = Mocktail.explain(overrides_everything)
stubbings = explanation.reference.stubbings
assert_equal stubbings[0]&.satisfaction_count, 1
assert_equal stubbings[1]&.satisfaction_count, 1
assert_equal stubbings[2]&.satisfaction_count, 1
end
sig { void }
def test_passing_mocks_and_comparing_them
mock_1 = Mocktail.of(OverridesEverything)
mock_2 = Mocktail.of(OverridesEverything)
stubs { mock_1.normal_method(mock_2) }.with { :great_success }
assert_equal mock_1.normal_method(mock_2), :great_success
end
class OverridesNil
extend T::Sig
sig { returns(T.untyped) }
def nil?
false
end
end
sig { void }
def test_overrides_nil?
skip "Can't quite figure out how to fix this"
# This fails because when an attribute is set to a T::Struct that overrides
# `nil?` (and just returns `nil?`), this check here kicks off an infinite
# recursion:
#
# https://github.com/sorbet/sorbet/blob/master/gems/sorbet-runtime/lib/types/props/private/setter_factory.rb#L116
overrides_nil = Mocktail.of(OverridesNil)
overrides_nil.nil?
end
end