我有一个函数im测试,假设返回错误500但是在将'http_errors' => 'false'put定义后,返回的错误从500变为404.这是我的函数:

public function testApiAd_updateWithIllegalGroupId($adId) 
    $client = new Client(['base_uri' => self::$base_url]);
    try {
        $response = $client->put(self::$path.$adId, ['form_params' => [
          'name' => 'bellow content - guzzle testing',
          'description' => 'guzzle testing ad - demo',
          'group_id' => '999999999',
    } catch (Guzzle\Http\Exception\BadResponseException $e) {
        //Here i want to compare received error to 500

现在这个函数将返回server error: 500但它也阻止类执行其余的测试,我无法断言它。 我怎么能在我的函数中使用guzzle getStatusCode()获取错误500而不是404,如上所述


BadResponseException包含原始Request和Response对象。 所以你可以,catch阻止以下断言:

} catch (Guzzle\Http\Exception\BadResponseException $e) {
        //Here i want to compare received error to 500
        $responseCode = $e->getResponse()->getStatusCode();
        $this->assertEquals(500, $responseCode, "Server Error");


author: matteo

参考更多解答: guzzle `http_errors` changes error from 500 to 404 ,转载请保留出处guzzle`http_errors`将错误从500更改为404及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.