From b68c75dda5d59c21375f3f6c0c385f7920fb04ad Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 16 Mar 2022 21:10:47 -0600 Subject: [PATCH] Add tests around problematic issues in NullDouble to Double --- spec/spectator/mocks/double_spec.cr | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/spec/spectator/mocks/double_spec.cr b/spec/spectator/mocks/double_spec.cr index 5443e0d..9a316b8 100644 --- a/spec/spectator/mocks/double_spec.cr +++ b/spec/spectator/mocks/double_spec.cr @@ -50,6 +50,30 @@ Spectator.describe Spectator::Double do end end + context "with abstract stubs and return type annotations" do + Spectator::Double.define(TestDouble) do + abstract_stub abstract def foo(value) : String + end + + let(arguments) { Spectator::Arguments.capture(/foo/) } + let(stub) { Spectator::ValueStub.new(:foo, "bar", arguments).as(Spectator::Stub) } + subject(dbl) { TestDouble.new([stub]) } + + it "enforces the return type" do + expect(dbl.foo("foobar")).to compile_as(String) + end + + it "raises on non-matching arguments" do + expect { dbl.foo("bar") }.to raise_error(Spectator::UnexpectedMessage, /foo/) + end + + it "raises on non-matching stub" do + stub = Spectator::ValueStub.new(:foo, 42, arguments).as(Spectator::Stub) + dbl._spectator_define_stub(stub) + expect { dbl.foo("foobar") }.to raise_error(TypeCastError, /String/) + end + end + context "with nillable return type annotations" do Spectator::Double.define(TestDouble) do abstract_stub abstract def foo : String?