diff --git a/spec/spectator/dsl/mocks/double_spec.cr b/spec/spectator/dsl/mocks/double_spec.cr index 60e3bd4..6c689fd 100644 --- a/spec/spectator/dsl/mocks/double_spec.cr +++ b/spec/spectator/dsl/mocks/double_spec.cr @@ -208,6 +208,16 @@ Spectator.describe "Double DSL" do end end + describe "predefined method stubs" do + double(:test8, foo: 42) + + let(dbl) { double(:test8, foo: 7) } + + it "overrides the original value" do + expect(dbl.foo).to eq(7) + end + end + describe "scope" do double(:outer, scope: :outer) double(:scope, scope: :outer) diff --git a/spec/spectator/dsl/mocks/null_double_spec.cr b/spec/spectator/dsl/mocks/null_double_spec.cr index d0a62dd..1219f50 100644 --- a/spec/spectator/dsl/mocks/null_double_spec.cr +++ b/spec/spectator/dsl/mocks/null_double_spec.cr @@ -176,4 +176,14 @@ Spectator.describe "Null double DSL" do end end end + + describe "predefined method stubs" do + double(:test8, foo: 42) + + let(dbl) { double(:test8, foo: 7).as_null_object } + + it "overrides the original value" do + expect(dbl.foo).to eq(7) + end + end end diff --git a/src/spectator/dsl/mocks.cr b/src/spectator/dsl/mocks.cr index 377da5b..7b02324 100644 --- a/src/spectator/dsl/mocks.cr +++ b/src/spectator/dsl/mocks.cr @@ -95,7 +95,7 @@ module Spectator::DSL found_tuple = found_tuples.last %} {% if found_tuple %} - {{found_tuple[2].id}}.new + {{found_tuple[2].id}}.new({{**value_methods}}) {% else %} ::Spectator::LazyDouble.new({{name}}, {{**value_methods}}) {% end %} diff --git a/src/spectator/mocks/double.cr b/src/spectator/mocks/double.cr index c3af323..03d974f 100644 --- a/src/spectator/mocks/double.cr +++ b/src/spectator/mocks/double.cr @@ -4,6 +4,7 @@ require "./stub" require "./stubbable" require "./stubbed_name" require "./unexpected_message" +require "./value_stub" module Spectator # Stands in for an object for testing that a SUT calls expected methods. @@ -76,6 +77,15 @@ module Spectator def initialize(@stubs : Array(Stub) = [] of Stub) end + # Creates the double. + # + # An initial set of stubs can be provided with *value_methods*. + def initialize(**value_methods) + @stubs = value_methods.map do |key, value| + ValueStub.new(key, value).as(Stub) + end + end + # Compares against another object. # # Always returns false.