mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add ExceptionStub
This commit is contained in:
parent
f17cc73487
commit
4d5004ab4f
5 changed files with 354 additions and 0 deletions
44
src/spectator/mocks/exception_stub.cr
Normal file
44
src/spectator/mocks/exception_stub.cr
Normal file
|
@ -0,0 +1,44 @@
|
|||
require "../location"
|
||||
require "./arguments"
|
||||
require "./stub"
|
||||
require "./stub_modifiers"
|
||||
|
||||
module Spectator
|
||||
# Stub that raises an exception.
|
||||
class ExceptionStub < Stub
|
||||
# Invokes the stubbed implementation.
|
||||
def call(call : MethodCall) : Nil
|
||||
raise @exception
|
||||
end
|
||||
|
||||
# Creates the stub.
|
||||
def initialize(method : Symbol, @exception : Exception, constraint : AbstractArguments? = nil, location : Location? = nil)
|
||||
super(method, constraint, location)
|
||||
end
|
||||
end
|
||||
|
||||
module StubModifiers
|
||||
# Returns a new stub that raises an exception.
|
||||
def and_raise(exception : Exception)
|
||||
ExceptionStub.new(method, exception, constraint, location)
|
||||
end
|
||||
|
||||
# :ditto:
|
||||
def and_raise(exception_class : Exception.class, message)
|
||||
exception = exception_class.new(message)
|
||||
and_raise(exception)
|
||||
end
|
||||
|
||||
# :ditto:
|
||||
def and_raise(message : String? = nil)
|
||||
exception = Exception.new(message)
|
||||
and_raise(exception)
|
||||
end
|
||||
|
||||
# :ditto:
|
||||
def and_raise(exception_class : Exception.class)
|
||||
exception = exception_class.new
|
||||
and_raise(exception)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue