How do I test for an exact Exception message, rather than a substring, with PHPUnit?

后端 未结 6 1721
难免孤独
难免孤独 2021-01-12 07:14

According to the PHPUnit Documentation on @expectedExceptionMessage, the string must only be a substring of the actual Exception thrown.

In

6条回答
  •  孤城傲影
    2021-01-12 07:31

    If your test class extends PHPUnit_Framework_TestCase, you should be able to use setExpectedException method:

        /**
         * @param mixed  $exceptionName
         * @param string $exceptionMessage
         * @param int    $exceptionCode
         *
         * @since  Method available since Release 3.2.0
         */
        public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = null)
        {
            $this->expectedException        = $exceptionName;
            $this->expectedExceptionMessage = $exceptionMessage;
            $this->expectedExceptionCode    = $exceptionCode;
        }
    

    It allows you to assert an exception class alone, exception message and even exception code.

提交回复
热议问题