2022-07-13 05:22:47 +00:00
|
|
|
require "../spec_helper"
|
|
|
|
|
2022-12-18 04:01:22 +00:00
|
|
|
module Readme
|
|
|
|
abstract class Interface
|
|
|
|
abstract def invoke(thing) : String
|
|
|
|
end
|
2022-07-13 05:22:47 +00:00
|
|
|
|
2022-12-18 04:01:22 +00:00
|
|
|
# Type being tested.
|
|
|
|
class Driver
|
|
|
|
def do_something(interface : Interface, thing)
|
|
|
|
interface.invoke(thing)
|
|
|
|
end
|
2022-07-13 05:22:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-18 04:01:22 +00:00
|
|
|
Spectator.describe Readme::Driver do
|
2022-07-13 05:22:47 +00:00
|
|
|
# Define a mock for Interface.
|
2022-12-18 04:01:22 +00:00
|
|
|
mock Readme::Interface
|
2022-07-13 05:22:47 +00:00
|
|
|
|
|
|
|
# Define a double that the interface will use.
|
|
|
|
double(:my_double, foo: 42)
|
|
|
|
|
|
|
|
it "does a thing" do
|
|
|
|
# Create an instance of the mock interface.
|
2022-12-18 04:01:22 +00:00
|
|
|
interface = mock(Readme::Interface)
|
2022-07-13 05:22:47 +00:00
|
|
|
# Indicate that `#invoke` should return "test" when called.
|
|
|
|
allow(interface).to receive(:invoke).and_return("test")
|
|
|
|
|
|
|
|
# Create an instance of the double.
|
|
|
|
dbl = double(:my_double)
|
|
|
|
# Call the mock method.
|
|
|
|
subject.do_something(interface, dbl)
|
|
|
|
# Verify everything went okay.
|
|
|
|
expect(interface).to have_received(:invoke).with(dbl)
|
|
|
|
end
|
|
|
|
end
|