PHPUnit 7:无法断言抛出类型为\ InvalidArgumentException的异常

问题描述 投票:1回答:2

我有这个代码:

public function method(){
  //...
  if(!$exist) {
      throw new \InvalidArgumentException('Ce client inexistant', 400);
  }
}

我做这段代码的UT:

public function methodTest(){
      //...
      if(!$exist) {
          $this->expectExceptionMessage("Ce client inexistant");
          $this->expectException("\InvalidArgumentException");
      }
    }

它显示错误消息

无法断言抛出类型“\ InvalidArgumentException”的异常。

我不知道我的代码中的错误在哪里。

php phpunit
2个回答
0
投票

尝试

      $this->expectException(\InvalidArgumentException::class);

代替:

      $this->expectException("\InvalidArgumentException");

希望这有帮助


0
投票

我解决了我的问题。这是代码:

$ this-> throwException(new \ InvalidArgumentException('Ce client inexistant',400));

© www.soinside.com 2019 - 2024. All rights reserved.