簡體   English   中英

PHPUnit 7:無法斷言拋出類型為 \\InvalidArgumentException 的異常

[英]PHPUnit 7: Failed asserting that exception of type \InvalidArgumentException is thrown

我有這個代碼:

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”的異常。

我不知道我的代碼中的錯誤在哪里。

對於遇到此問題的人,請嘗試以下操作:

    $this->expectException(Exception::class);

    $client = self::createClient();
    $client->catchExceptions(false);

    $client->request(...);

嘗試

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

代替:

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

希望這個幫助

我解決了我的問題。 這是代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM