{"record":{"id":"09dc85a8ce313dd2","repo":"sebastianbergmann/phpunit","slug":"failed-asserting-that-exception-with-code-s-is","errorCode":null,"errorMessage":"Failed asserting that exception with code \"%s\" is thrown","messagePattern":"Failed asserting that exception with code \"(.+?)\" is thrown","errorType":"exception","errorClass":"AssertionFailedError","httpStatus":null,"severity":"error","filePath":"src/Framework/TestCase/ExceptionExpectation.php","lineNumber":172,"sourceCode":"                sprintf(\n                    'Failed asserting that exception with message %s \"%s\" is thrown',\n                    $this->expectedMessageConstraint instanceof ExceptionMessageIs ? 'is' : 'containing',\n                    $this->expectedMessage,\n                ),\n            );\n        } elseif ($this->expectedMessageRegularExpression !== null) {\n            $test->addToAssertionCount(1);\n\n            throw new AssertionFailedError(\n                sprintf(\n                    'Failed asserting that exception with message matching \"%s\" is thrown',\n                    $this->expectedMessageRegularExpression,\n                ),\n            );\n        } elseif ($this->expectedCode !== null) {\n            $test->addToAssertionCount(1);\n\n            throw new AssertionFailedError(\n                sprintf(\n                    'Failed asserting that exception with code \"%s\" is thrown',\n                    $this->expectedCode,\n                ),\n            );\n        }\n    }\n}\n","sourceCodeStart":154,"sourceCodeEnd":181,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Framework/TestCase/ExceptionExpectation.php#L154-L181","documentation":"Thrown by ExceptionExpectation::assertWasRaised() when expectExceptionCode() was configured but no exception was raised during the test. With nothing thrown there is no code to compare, so PHPUnit fails the test with the expected code in the message.","triggerScenarios":"Calling $this->expectExceptionCode(123) while the code under test runs to completion without throwing.","commonSituations":"Guards added to production code so the error no longer happens; tests for error codes where the error-triggering input was corrected; exception thrown in a dependent service that is now mocked to succeed.","solutions":["Ensure the test input/arrangement actually causes the exception","Remove expectExceptionCode() (and related expectations) if the non-throwing behavior is now correct, and assert the return value instead"],"exampleFix":"// before\n$this->expectExceptionCode(404);\n$repo->find($knownGoodId); // found, no exception\n\n// after\n$this->expectExceptionCode(404);\n$repo->find($missingId);","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Arrange inputs that actually produce the exception whose code you expect","Delete stale expectExceptionCode() calls when the corresponding error path is removed"],"tags":["phpunit","exception-assertion","error-code","expected-exception"],"backgroundTag":"expected-exception-code-not-thrown","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}