Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在laravel单元测试中获得响应数据_Laravel_Unit Testing - Fatal编程技术网

如何在laravel单元测试中获得响应数据

如何在laravel单元测试中获得响应数据,laravel,unit-testing,Laravel,Unit Testing,我一直在尝试使用laravel单元测试。 我想要的是如何获得原始的响应数据 public function testProducts() { $response=$this->call('GET','/products',['cat','all']); // dd($response); $data = $response->getData(); // dd($data); } 当我在第一种情况下添加dd时,它会显示HTML内容。我想展示的是数

我一直在尝试使用laravel单元测试。 我想要的是如何获得原始的响应数据

public function testProducts()
{
    $response=$this->call('GET','/products',['cat','all']);

    // dd($response);

    $data = $response->getData();
    //  dd($data);
}

当我在第一种情况下添加dd时,它会显示HTML内容。我想展示的是数组的真实内容。我该怎么办?

您不需要取出数据。而是使用
assertJson()
。您只需将
JSON
结构构建为
PHP
关联数组,它将通过
JSON
编码来断言它

$response->assertJson([
    [
        'name' => 'Iphone',
    ]
]);

你不需要把数据拿出来。而是使用
assertJson()
。您只需将
JSON
结构构建为
PHP
关联数组,它将通过
JSON
编码来断言它

$response->assertJson([
    [
        'name' => 'Iphone',
    ]
]);

首先在同一测试用例上创建不同的产品,然后使用assertJSon进行检查

factory(product::class)->create(['name' => 'Cat']);
factory(product::class)->create(['name' => 'all']);
factory(product::class)->create(['name' => 'other']);



$response = $this->json('GET', '/api/products', ['Cat','all'])
    ->assertStatus(200)
    ->assertJson([
        [ 'name' => 'cat'],['name'=>'all']
    ]);

首先在同一测试用例上创建不同的产品,然后使用assertJSon进行检查

factory(product::class)->create(['name' => 'Cat']);
factory(product::class)->create(['name' => 'all']);
factory(product::class)->create(['name' => 'other']);



$response = $this->json('GET', '/api/products', ['Cat','all'])
    ->assertStatus(200)
    ->assertJson([
        [ 'name' => 'cat'],['name'=>'all']
    ]);

什么是真正的内容?您正在从路线返回视图?很抱歉,因为在laravel测试中是新手。不,我不返回视图。我返回一个包含产品的数组。什么是真正的内容?您正在从路线返回视图?很抱歉,因为在laravel测试中是新手。不,我不返回视图。我返回一个包含产品的数组。