Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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
Php 拉维尔';s函数测试JSON API失败,附加文件时出错_Php_Laravel 5_Phpunit_Functional Testing_Laravel 5.2 - Fatal编程技术网

Php 拉维尔';s函数测试JSON API失败,附加文件时出错

Php 拉维尔';s函数测试JSON API失败,附加文件时出错,php,laravel-5,phpunit,functional-testing,laravel-5.2,Php,Laravel 5,Phpunit,Functional Testing,Laravel 5.2,因此,我编写了一个用于测试上传功能的功能测试,但失败了,出现以下错误: 1) UploadTest::testValidVideoUpload Error: Call to a member function filter() on null /.../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:765 /.../vendor/laravel/framewo

因此,我编写了一个用于测试上传功能的功能测试,但失败了,出现以下错误:

1) UploadTest::testValidVideoUpload
Error: Call to a member function filter() on null

/.../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:765
/.../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:737
/.../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:718
/.../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:639
/.../tests/ShareTests/UploadTest.php:19
代码是:

public function testValidVideoUpload()
{
    $this->post(route('share.upload'), ['type' => 'video'])
        ->attach($this->test_case_files_path . 'testcase.mp4', 'file')
        ->seeJson(['error' => false]);
}
我正在使用:

  • 拉维5.2稳定
  • phpUnit 5.2.2
  • PHP7.0.3-2+deb.sury.org~trusty+1(cli)(NTS)
编辑:
我正在RESTful服务上使用这些测试。我们可以使用
attach
方法吗???因为我查看了它的实现,它使用DOM解析器和CSS选择器来确定输入名称是否确实存在于响应中

问题出在这里。显然(我还不确定,因为文档中根本没有说明),在测试JSON API时不能使用
attach
方法。下面是我如何解决这个问题的。而不是

    $this->post(route('share.upload'), ['type' => 'video'],
        ['HTTP_Authorization' => "Bearer " . $token])
        ->attach($this->test_case_files_path . 'testcase.mp4', 'file')
        ->seeJson(['error' => false]);
我用了这个:

$uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(
            $file, 'testcase.mp4', 'video/mp4', filesize($file), null, true
        );
        $this->response = $this->call('post', route('share.upload'), ['type' => 'video'], [],
            ['file' => $uploadedFile],
            $this->transformHeadersToServerVars([
                'Authorization' => "Bearer " . $token,
                'Content-type' => 'multipart/form-data',
            ]));
        $this->seeJson(['error' => false]);

您正在测试的方法的内容是什么?如果我使用REST客户端,上传将成功。所以我很确定问题不在这里。所以我几乎可以肯定这对你没有帮助。你认为会吗?能够看到主题代码是all@JakubZalas:我对单元测试还不熟悉,但我很确定我说的是单元测试:|。每个
test
测试一条路由,该路由由一个方法处理,因此命名为:单元测试。如果我错了,请纠正我。谢谢:-)你错了:)单元测试是对应用程序中最小的可测试部分的测试。在OOP中,这些是对类和方法的测试。单元测试也是隔离的,所以测试单个单元时要与其他单元隔离。其他任何东西都是集成测试。您的测试贯穿整个框架(路由等),调用您的控制器和其他协作者。这绝对不是单元测试。