From e52f187eb79c36807d7734545f30fbe52936d4ac Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Tue, 12 Jul 2022 23:22:47 -0600 Subject: [PATCH] Show new mock and double system in README --- README.md | 39 ++++++++++++++++++++++++++++++--------- spec/docs/readme_spec.cr | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 spec/docs/readme_spec.cr diff --git a/README.md b/README.md index 78a6b17..c75f127 100644 --- a/README.md +++ b/README.md @@ -255,19 +255,40 @@ end Spectator supports an extensive mocking feature set via two types - mocks and doubles. Mocks are used to override behavior in existing types. Doubles are objects that stand-in when there are no type restrictions. -Stubs can be defined on both that control how methods behave. +Stubs can be defined on both which control how methods behave. ```crystal -double :my_double do - stub foo : Int32 - stub bar(arg) { arg.to_s } +abstract class Interface + abstract def invoke(thing) : String end -it "does a thing" do - dbl = double(:my_double) - allow(dbl).to receive(:foo).and_return(42) - expect(dbl.foo).to eq(42) - expect(dbl.bar(42)).to eq("42") +# Type being tested. +class Driver + def do_something(interface : Interface, thing) + interface.invoke(thing) + end +end + +Spectator.describe Driver do + # Define a mock for Interface. + mock Interface + + # 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. + interface = mock(Interface) + # 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(thing) + end end ``` diff --git a/spec/docs/readme_spec.cr b/spec/docs/readme_spec.cr new file mode 100644 index 0000000..6024906 --- /dev/null +++ b/spec/docs/readme_spec.cr @@ -0,0 +1,34 @@ +require "../spec_helper" + +private abstract class Interface + abstract def invoke(thing) : String +end + +# Type being tested. +private class Driver + def do_something(interface : Interface, thing) + interface.invoke(thing) + end +end + +Spectator.describe Driver do + # Define a mock for Interface. + mock Interface + + # 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. + interface = mock(Interface) + # 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