Laravel 单元测试 - POST 请求有效负载

Don*_*joe 5 php phpunit laravel

我需要帮助为 Laravel 创建单元测试用例。它是一个 API 端点,接受 json 作为 POST 有效负载(不是 POST 表单),并且基本上只是基于该 json 对象在数据库中创建一个条目。API 工作正常,但我在创建单元测试时遇到问题。

控制器代码包含

$request = Request::instance();
$content = $request->getContent();
$inputRequest = json_decode($content);
Run Code Online (Sandbox Code Playgroud)

我不知道如何将有效负载传递到单元测试代码中(扩展 Illuminate\Foundation\Testing\TestCase

任何人都可以提供帮助?

谢谢堆

小智 4

在单元测试中使用laravel 提供的call()方法时。

第六个参数 $content(String) 可用于传入测试用例类中的 post 负载:

$this->call($method, $uri, $parameters, $files, $server, jsonencode($yourdata), $changeHistory);
Run Code Online (Sandbox Code Playgroud)