0

I want to test my thrown exception, but test the exception class only, and not have to test the exception message. I saw this thread which was helpful, but didn't specifically address this: PHPUnit assert that an exception was thrown?

This assertion: $this->expectException(NotFoundException::class);

Leads to an error: Failed asserting that exception message is empty but is 'EXCEPTION MESSAGE'.

Is there a way for me to test ONLY the exception class, and NOT test the exception message? Or do I have to instead test the exception code ($this->expectExceptionCode(404)?

(I am on PHPUnit 5.7)

Thanks!

y mg
  • 13
  • 3
  • 1
    Strange. That method should only test the exception class. There's explicitly an `expectExceptionMessage` method to test the message. Can't find any sources which would explain if the behaviour was different back in 5.7. – El_Vanja Mar 05 '21 at 22:04
  • 5.7 was end-of-lifed three years ago. I'm pretty sure that it used custom PHPDoc attributes to indicate an expected exception, and not a method call. – Alex Howansky Mar 05 '21 at 22:16
  • Ahh ok looks like 5.7 supported both the phpdoc tag and the explicit method call. Do you also have `@expectedExceptionMessage` in the test method's doc block? – Alex Howansky Mar 05 '21 at 22:19
  • Had a brief look at the 5.7 source code, see `src/Framework/Constraint/ExceptionMessage.php` -- you can not get this error message unless you call `$this->expectExceptionMessage()` or have `@expectedExceptionMessage` in your doc block. – Alex Howansky Mar 05 '21 at 22:39
  • Thanks for responses all! I do not have the `@expectedExceptionMessage` phpdoc tag, and have not called `$this->expectExceptionMessage` either. So this is weird... do you think it has to do with the EOL? I really don't want to use 5.7 but I can't use the newer versions bc of compatibility issues. Also further info - I'm testing on a cakephp2.10.x application – y mg Mar 06 '21 at 00:19
  • Are you extending a custom `TestCase` parent class by any chance? That seemed to be the issue [here](https://github.com/sebastianbergmann/phpunit/issues/2809#issuecomment-336741817) – PtrTon Mar 06 '21 at 00:24
  • hm no, I don't think so. My test has: `class sampleTest extends CakeTestCase`. But thank you for your suggestion! – y mg Mar 06 '21 at 00:36
  • `CakeTestCase` is a custom `TestCase` – Sebastian Bergmann Mar 06 '21 at 09:02
  • If you read the [docs](http://www.apimirror.com/cakephp~2.10/class-caketestcase#_expectException), you can see it says "Compatibility wrapper function for setExpectedException". `setExpectedException` is an old method which expected both the type and message. It's probably passing a default value of null or empty string into it, causing this behaviour. – El_Vanja Mar 08 '21 at 13:28
  • @SebastianBergmann sorry, my mistake. Thank you for that clarification. – y mg Mar 08 '21 at 22:37
  • @El_Vanja thank you for your response and link to the reference docs, that is helpful. Yes, the method expecting the type and message explains the issue. – y mg Mar 08 '21 at 22:37

0 Answers0