dthhlf1777 2016-02-09 05:44
浏览 128

如何在Laravel中模拟文件上传

I am using a 3rd party API to manage my uploaded images, I have abstracted this API with my own class, and also written class to test file upload, but the files get uploaded to the web every time I run the test, I will like to prevent this and use mock to test that the upload function is being called instead.

How do I go about this?

  • 写回答

1条回答 默认 最新

  • doutang3760 2016-02-22 19:26
    关注

    Here is what I do.

    I create a UploadedFile isntance (mock a uploaded file) and pass this to my Service Class - This is my approach for testing my Service Class that handles file uploads.

    If you want to test the Controller method, I suggest you go with a Acceptance Test and use a real browser to send the request. I could'nt find a decent way of doing this.

    //Mock a uploaded file
      $uploadedFile = new UploadedFile(codecept_data_dir().'/attachments/temporary/' . $filename, $filename, $mimeType, $size);
    

    You will need to use \Symfony\Component\HttpFoundation\File\UploadedFile; on top.

    评论

报告相同问题?