Provide default stub for Process.exit

Prevent terminating the process from within a test.
This commit is contained in:
Michael Miller 2021-07-10 14:18:07 -06:00
parent 294bd61a25
commit 5f61a24656
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
2 changed files with 16 additions and 0 deletions

View file

@ -1,4 +1,5 @@
require "./mocks/*"
require "./system_exit"
module Spectator
# Functionality for mocking existing types.
@ -11,3 +12,13 @@ module Spectator
end
end
end
# Add default stub to `exit` method.
# This captures *most* (technically not all) attempts to exit the process.
# This stub only takes effect in example code.
# It intercepts `exit` calls and raises `Spectator::SystemExit` to prevent killing the test.
class ::Process
include ::Spectator::Mocks::Stubs
stub self.exit(code) { raise ::Spectator::SystemExit.new }
end

View file

@ -0,0 +1,5 @@
module Spectator
# Exception raised when `exit` is called and intercepted from a stub.
class SystemExit < Exception
end
end