Laravel5.8 使用 phpunit 测试报错

环境:

Mac 10.14.4 + php 7.3.4 + laravel 5.8 + PHPUnit 6.5.3

控制器代码:

namespace App\Http\Controllers\Admin;
class TestController extends Controller 
{
    public function index() 
    {
        return ['success' => true];
    }
}

测试代码:

namespace Tests\Feature;
use Tests\TestCase;
class UserTest extends TestCase
{
    public function testLists()
    {
        $response = $this->json('GET', '/admin/test');
        // dd($response);
        $response->assertStatus(200);
    }
}

Postman直接请求接口:

laravel5.8使用phpunit测试报错

运行phpunit命令:

laravel5.8使用phpunit测试报错

打印 $response

PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

Illuminate\Foundation\Testing\TestResponse {#462
  +baseResponse: Illuminate\Http\JsonResponse {#521
    #data: "{"success":true}"
    #callback: null
    #encodingOptions: 0
    +headers: Symfony\Component\HttpFoundation\ResponseHeaderBag {#520
      #computedCacheControl: array:2 [
        "no-cache" => true
        "private" => true
      ]
      #cookies: []
      #headerNames: array:3 [
        "cache-control" => "Cache-Control"
        "date" => "Date"
        "content-type" => "Content-Type"
      ]
      #headers: array:3 [
        "cache-control" => array:1 [
          0 => "no-cache, private"
        ]
        "date" => array:1 [
          0 => "Fri, 26 Jul 2019 13:46:15 GMT"
        ]
        "content-type" => array:1 [
          0 => "application/json"
        ]
      ]
      #cacheControl: []
    }
    #content: "{"success":true}"
    #version: "1.1"
    #statusCode: 200
    #statusText: "OK"
    #charset: null
    +original: array:1 [
      "success" => true
    ]
    +exception: null
  }
  #streamedContent: null
}

第一次使用phpunit,看到这个问题完全没有思路,不知道应该从那里调试,希望能提供一个思路,我能继续研究下去! 非常感谢!

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 1

phpunit 升级到 ^7.5

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!