From f17cc73487c918c8d7c9d83bbe6440174ba80e90 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 9 Jul 2022 21:48:22 -0600 Subject: [PATCH] More multi value stub tests --- spec/spectator/mocks/multi_value_stub_spec.cr | 31 ++++++++++++++++++- spec/spectator/mocks/null_stub_spec.cr | 24 ++++++++++++++ spec/spectator/mocks/value_stub_spec.cr | 24 ++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/spec/spectator/mocks/multi_value_stub_spec.cr b/spec/spectator/mocks/multi_value_stub_spec.cr index 4b91085..7732c42 100644 --- a/spec/spectator/mocks/multi_value_stub_spec.cr +++ b/spec/spectator/mocks/multi_value_stub_spec.cr @@ -18,13 +18,18 @@ Spectator.describe Spectator::MultiValueStub do values = Array.new(3) { stub.call(method_call) } expect(values).to eq([3, 5, 7]) end + + it "returns the final value after exhausting other values" do + values = Array.new(5) { stub.call(method_call) } + expect(values).to eq([3, 5, 7, 7, 7]) + end end context Spectator::StubModifiers do describe "#and_return(value)" do let(arguments) { Spectator::Arguments.capture(/foo/) } let(location) { Spectator::Location.new(__FILE__, __LINE__) } - let(original) { Spectator::ValueStub.new(:foo, 42, arguments, location) } + let(original) { Spectator::MultiValueStub.new(:foo, [3, 5, 7], arguments, location) } subject(stub) { original.and_return(123) } it "produces a stub that returns a value" do @@ -43,6 +48,30 @@ Spectator.describe Spectator::MultiValueStub do expect(stub.location).to eq(location) end end + + describe "#and_return(*values)" do + let(arguments) { Spectator::Arguments.capture(/foo/) } + let(location) { Spectator::Location.new(__FILE__, __LINE__) } + let(original) { Spectator::MultiValueStub.new(:foo, [3, 5, 7], arguments, location) } + subject(stub) { original.and_return(3, 2, 1, 0) } + + it "produces a stub that returns values" do + values = Array.new(5) { stub.call(method_call) } + expect(values).to eq([3, 2, 1, 0, 0]) + end + + it "retains the method name" do + expect(stub.method).to eq(:foo) + end + + it "retains the arguments constraint" do + expect(stub.constraint).to eq(arguments) + end + + it "retains the location" do + expect(stub.location).to eq(location) + end + end end describe "#===" do diff --git a/spec/spectator/mocks/null_stub_spec.cr b/spec/spectator/mocks/null_stub_spec.cr index 021c0c5..d919aea 100644 --- a/spec/spectator/mocks/null_stub_spec.cr +++ b/spec/spectator/mocks/null_stub_spec.cr @@ -40,6 +40,30 @@ Spectator.describe Spectator::NullStub do expect(stub.location).to eq(location) end end + + describe "#and_return(*values)" do + let(arguments) { Spectator::Arguments.capture(/foo/) } + let(location) { Spectator::Location.new(__FILE__, __LINE__) } + let(original) { Spectator::NullStub.new(:foo, arguments, location) } + subject(stub) { original.and_return(3, 2, 1, 0) } + + it "produces a stub that returns values" do + values = Array.new(5) { stub.call(method_call) } + expect(values).to eq([3, 2, 1, 0, 0]) + end + + it "retains the method name" do + expect(stub.method).to eq(:foo) + end + + it "retains the arguments constraint" do + expect(stub.constraint).to eq(arguments) + end + + it "retains the location" do + expect(stub.location).to eq(location) + end + end end describe "#===" do diff --git a/spec/spectator/mocks/value_stub_spec.cr b/spec/spectator/mocks/value_stub_spec.cr index 4c34b1e..7624188 100644 --- a/spec/spectator/mocks/value_stub_spec.cr +++ b/spec/spectator/mocks/value_stub_spec.cr @@ -40,6 +40,30 @@ Spectator.describe Spectator::ValueStub do expect(stub.location).to eq(location) end end + + describe "#and_return(*values)" do + let(arguments) { Spectator::Arguments.capture(/foo/) } + let(location) { Spectator::Location.new(__FILE__, __LINE__) } + let(original) { Spectator::ValueStub.new(:foo, 42, arguments, location) } + subject(stub) { original.and_return(3, 2, 1, 0) } + + it "produces a stub that returns values" do + values = Array.new(5) { stub.call(method_call) } + expect(values).to eq([3, 2, 1, 0, 0]) + end + + it "retains the method name" do + expect(stub.method).to eq(:foo) + end + + it "retains the arguments constraint" do + expect(stub.constraint).to eq(arguments) + end + + it "retains the location" do + expect(stub.location).to eq(location) + end + end end describe "#===" do