Implement null doubles

This commit is contained in:
Michael Miller 2019-11-11 23:40:08 -07:00
parent 48ea53c2f5
commit 94e210c306
2 changed files with 31 additions and 3 deletions

View file

@ -10,8 +10,34 @@ module Spectator::DSL
end
{% else %}
class Double{{name.id}} < ::Spectator::Mocks::Double
def initialize
super({{name.id.symbolize}})
def initialize(null = false)
super({{name.id.symbolize}}, null)
end
def as_null_object
Double{{name.id}}.new(true)
end
{{block.body}}
end
{% end %}
end
macro null_double(name, **stubs, &block)
{% if block.is_a?(Nop) %}
Double{{name.id}}.new(true).tap do |%double|
{% for name, value in stubs %}
allow(%double).to receive({{name.id}}).and_return({{value}})
{% end %}
end
{% else %}
class Double{{name.id}} < ::Spectator::Mocks::Double
def initialize(null = true)
super({{name.id.symbolize}}, null)
end
def as_null_object
Double{{name.id}}.new(true)
end
{{block.body}}

View file

@ -4,7 +4,7 @@ require "./unexpected_message_error"
module Spectator::Mocks
abstract class Double
def initialize(@spectator_double_name : Symbol)
def initialize(@spectator_double_name : Symbol, @null = false)
end
private macro stub(definition, &block)
@ -74,6 +74,8 @@ module Spectator::Mocks
end
macro method_missing(call)
return self if @null
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{call.name}}")
end