mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Implement null doubles
This commit is contained in:
parent
48ea53c2f5
commit
94e210c306
2 changed files with 31 additions and 3 deletions
|
@ -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}}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue