Implement stub that raises

This commit is contained in:
Michael Miller 2019-11-17 12:53:18 -07:00
parent 6e287f864b
commit b896a7f1d5
2 changed files with 30 additions and 0 deletions

View file

@ -0,0 +1,14 @@
require "./generic_arguments"
require "./generic_method_stub"
module Spectator::Mocks
class ExceptionMethodStub(ExceptionType) < GenericMethodStub(Nil)
def initialize(name, source, @exception : ExceptionType, args = nil)
super(name, source, args)
end
def call(_args : GenericArguments(T2, NT2), rt : RT.class) forall T2, NT2, RT
raise @exception
end
end
end

View file

@ -20,6 +20,22 @@ module Spectator::Mocks
MultiValueMethodStub.new(@name, @source, values.to_a, @args)
end
def and_raise(exception_type : Exception.class)
ExceptionMethodStub.new(@name, @source, exception_type.new, @args)
end
def and_raise(exception : Exception)
ExceptionMethodStub.new(@name, @source, exception, @args)
end
def and_raise(message : String)
ExceptionMethodStub.new(@name, @source, Exception.new(message), @args)
end
def and_raise(exception_type : Exception.class, *args) forall T
ExceptionMethodStub.new(@name, @source, exception_type.new(*args), @args)
end
def with(*args : *T, **opts : **NT) forall T, NT
args = GenericArguments.new(args, opts)
NilMethodStub.new(@name, @source, args)