-
Notifications
You must be signed in to change notification settings - Fork 43
/
9_30.rb
76 lines (62 loc) · 1.75 KB
/
9_30.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
# Updating the production API, and then injecting a Double that still uses the old API.
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring:, cog:, wheel:)
@chainring = chainring
@cog = cog
@wheel = wheel
end
def gear_inches
ratio * wheel.diameter
end
def ratio
chainring / cog.to_f
end
end
class Wheel
attr_reader :rim, :tire
def initialize(rim, tire)
@rim = rim
@tire = tire
end
def width # <- Here's where the real API changed
rim + (tire * 2)
end
end
# Gear expects a 'Duck' that knows 'diameter'
Gear.new(
chainring: 52,
cog: 11,
wheel: Wheel.new(26, 1.5)).gear_inches rescue "died"
##########################################################
require "minitest"
require "minitest/reporters"
require "minitest/asciidoc_plugin"
Minitest::Reporters.use! Minitest::Reporters::Asciidoc.new
##### Remove the Gear test's dependency on the Wheel class by
##### injecting something that HONORS the diameterable role,
##### instead of BEING the Wheel class.
#####
# Create a player of the 'Diameterizable' role
class DiameterDouble
def diameter # <- Wrong implementation of
10 # <- Diameterizable API
end
end
class GearTest < Minitest::Test
def test_calculates_gear_inches
gear = Gear.new(
chainring: 52,
cog: 11,
wheel: DiameterDouble.new)
assert_in_delta(47.27,
gear.gear_inches,
0.01)
end
end
Minitest.run
**[rubyclass]#GearTest#**
**[green]#PASS#** __(0.00s)__ test_calculates_gear_inches
Finished in 0.00015s
1 tests, 1 assertions, [green]#0 failures, 0 errors#, [yellow]#0 skips#
Minitest::Runnable.reset