From ef62e2824504f5b219379c224c43a5e8d119d270 Mon Sep 17 00:00:00 2001 From: Beau Simensen Date: Wed, 23 Aug 2023 15:59:04 -0500 Subject: [PATCH] Fail if expected exception was not thrown --- src/Testing/GivenWhenThen/Scenario.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Testing/GivenWhenThen/Scenario.php b/src/Testing/GivenWhenThen/Scenario.php index a8e5746..5f0081a 100644 --- a/src/Testing/GivenWhenThen/Scenario.php +++ b/src/Testing/GivenWhenThen/Scenario.php @@ -170,6 +170,8 @@ public function assert(): self $handler = [$handler, 'handle']; } + $caughtException = false; + try { ($handler)($instance->when->command); } catch (\Throwable $throwable) { @@ -179,6 +181,8 @@ public function assert(): self } else { Assert::assertEquals($instance->expect, $throwable); } + + $caughtException = true; } else { throw $throwable; } @@ -186,6 +190,10 @@ public function assert(): self $recordedEvents = $messageRepository->lastCommit(); + if (isset($instance->expect) && !$caughtException) { + Assert::fail('Expected exception not thrown.'); + } + if (!isset($instance->outcome)) { Assert::fail('Expected an outcome.'); }