diff --git a/src/spectator.cr b/src/spectator.cr index 9ab920e..9e7f988 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -68,8 +68,11 @@ module Spectator # Silence default logger. ::Log.setup_from_env(default_level: :none) - # Build the spec and run it. + # Build the spec. spec = DSL::Builder.build + + # Run the spec with mocks enabled. + Mocks.enable spec.run rescue ex # Re-enable logger for fatal error. @@ -82,6 +85,8 @@ module Spectator # So we display a helpful error that could be reported and return non-zero. Log.fatal(exception: ex) { "Spectator encountered an unexpected error" } false + ensure + Mocks.disable end # Global configuration used by Spectator for running tests. diff --git a/src/spectator/harness.cr b/src/spectator/harness.cr index 1f9fa09..be60f40 100644 --- a/src/spectator/harness.cr +++ b/src/spectator/harness.cr @@ -136,11 +136,13 @@ module Spectator # Yields to run the test code and returns information about the outcome. # Returns a tuple with the elapsed time and an error if one occurred (otherwise nil). private def capture(&) : Tuple(Time::Span, Exception?) - error = nil - elapsed = Time.measure do - error = catch { yield } + Mocks::Scope.push do + error = nil + elapsed = Time.measure do + error = catch { yield } + end + {elapsed, error} end - {elapsed, error} end # Yields to run a block of code and captures exceptions.