我正在使用PHPUnit来测试我的API,但我不确定如何测试文件上传。
我看了this Laracast lesson,这让我很好地了解了如何使用Mockery模拟上传,但在我的情况下,我正在尝试测试API,无论出于何种原因,我都无法获得我对格林的考验。
我的测试代码:
namespace App;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Mockery as m;
class ApiResourcesControllerTest extends \TestCase
{
/** @test */
function a_user_can_upload_a_resource()
{
$file = m::mock(UploadedFile::class, [
'getClientOriginalName' => 'test.jpg'
]);
$file->shouldReceive('move') // the uploaded file is moved
->once()
->with('uploads', 'now-test.jpg');
$files = ['file' => $file]; // endpoint expects Input::file('file);
// this endpoint definitely works
$this->call('POST', 'api/resources/upload', [], [], $files);
$this->dump(); // for the mean time just dump the response
}
}
function time()
{
return 'now';
}
我知道api/resources/upload
端点工作正常,但我的测试代码返回以下错误:
Mockery\Exception\InvalidCountException: Method move("uploads", "now-test.jpg") from Mockery_0_Symfony_Component_HttpFoundation_File_UploadedFile should be called exactly 1 times but called 0 times.
我哪里错了?
非常感谢模拟文件上传到API的示例。