I'm trying to run this functional test on my laravel controller. I would like to test image processing, but to do so I want to fake image uploading. How do I do this? I found a few examples online but none seem to work for me. Here's what I have:

public function testResizeMethod()
{
    $this->prepareCleanDB();

    $this->_createAccessableCompany();

    $local_file = __DIR__ . '/test-files/large-avatar.jpg';

    $uploadedFile = new Symfony\Component\HttpFoundation\File\UploadedFile(
        $local_file,
        'large-avatar.jpg',
        'image/jpeg',
        null,
        null,
        true
    );


    $values =  array(
        'company_id' => $this->company->id
    );

    $response = $this->action(
        'POST',
        'FileStorageController@store',
        $values,
        ['file' => $uploadedFile]
    );

    $readable_response = $this->getReadableResponseObject($response);
}

但控制器没有通过这项判断:

elseif (!Input::hasFile('file'))
{
    return Response::error('No file uploaded');
}

So somehow the file isn't passed correctly. How do I go about this?

推荐答案

Docs for CrawlerTrait.html#method_action reads:

Parameters
string $method
string $action
array $wildcards
array $parameters
array $cookies
array $files
array $server
string $content

所以我想正确的判断应该是

$response = $this->action(
    'POST',
    'FileStorageController@store',
    [],
    $values,
    [],
    ['file' => $uploadedFile]
);

除非它需要非空通配符和cookie.

Laravel相关问答推荐

try 编写一个函数,如果产品存在,则无法删除特定类别

为什么在Blade 文件中输出用户通知时出现错误?

发送邮箱后,Laravel重定向偶尔会导致错误500

如何仅将日期列解析为月和日?

如何通过单击进入 vs 代码中的类

如何在 Laravel 中使用内存数据库的完整测试套件之前迁移和 seeder ?

判断 Laravel 中是否存在唯一索引键

使用 Eloquent (Laravel) 在 Group By 之前排序

Laravel 5.1-5.8 中以前的路由名称

如何仅在Blade模板存在时才包含它?

使用 Laravel 创建新项目会引发异常

在 Laravel 中将 Eloquent 导出到 Excel 时如何包含列标题?

Laravel 搜索关系

改变模式生成器中的列长度?

Guzzle 返回流空体而不是 json 体

Laravel 隐藏属性.例如密码 - 安全

在 Laravel 中包含 bootstrap 程序

Laravel 数据库模式中的 MediumBlob

在laravel中删除确认

如何在不要求用户登录 Laravel 的情况下验证Electron邮件