Laravel使用伪造的存储进行测试 [英] Laravel Testing with storage fake

查看:72
本文介绍了Laravel使用伪造的存储进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一种功能,可以下载基于数据库生成的CSV文件.我为此进行了测试,并且对我来说工作正常.但是问题是在完成测试运行后,该文件没有被删除.

I have developed a function to download CSV file which generated based on the database. I have created testing for this and working fine for me. but the problem is the file is not getting deleted after completing the test run.

问题.测试完全运行后,是否会自动删除使用存储伪造品创建的此文件?如果是的话,它不会为我删除.请查看我的测试功能.

Question. Will this file created using storage fake got deleted automatically once the test completely run? if yes it is not deleted for me. please look into my test function.

/*Test file*/
public function testAmazonDailyPendingStatusReport(){
    //creating factories    
    Storage::fake('reportslocal');  
    $dailyStatus = new DailyStatus(
       new FileWriter(),
       new Filesystem(),
       Storage::disk('reportslocal')
   );
   $fileExported = $dailyStatus->export();
   //continuing assertions
}

/*export function*/
public function export(){
     //fetch data from database.
     //create file using SplFileObject
     //writing files into it.
     //storing to 'reportslocal' path
     //sending email to client with attached this file
}

如果文件没有自动删除,该怎么办?或者我可以在测试函数中使用 Storage :: disk('reportslocal')-> delete($ fileExported).这是正确的方法吗?

If the file not deleted automatically, what should I do? or can I use Storage::disk('reportslocal')->delete($fileExported) in my test function . Is this a proper way?

这里要检查的最佳断言是什么?我检查了文件的存在,列号,列标题序列和值,检查了文件的内容.我有什么想念的吗?

What is the best assertion to be checked here? I have checked, the file existence, column number, column header sequence, and value, check the contents of the file. is there anything I missed?

请帮助我做到这一点(优先级是storage :: fake()问题.)

Please help me to do this(Priority is the storage::fake() issue.).

先谢谢了.

推荐答案

为什么 Storage :: fake()

Storage :: fake()用于在本地磁盘上设置目录以供测试套件使用.这有助于防止您修改实际定义的存储磁盘.

Why Storage::fake()

Storage::fake() is used to setup a directory on your local disk for your test suite to use. This helps keep you from modifying your actual defined storage disks.

例如,如果您的代码使用的是 s3 磁盘,其中所有操作都将击中已配置的AWS s3存储桶,则可以调用 Storage :: fake('s3'),它将用一个简单的本地磁盘交换出您的s3云配置,而无需修改您正在测试的代码.

If, for example, your code is using the s3 disk, where all operations will be hitting your configured AWS s3 bucket, you can call Storage::fake('s3'), and it will swap out your s3 cloud configuration with a simple local disk without having to modify the code you're testing at all.

现在,每次调用 Storage :: fake('reportslocal')时,将在调用该方法时清除已定义目录中的文件.但是,没有什么可以在测试完成后自动再次清除文件.

Now, every time you call Storage::fake('reportslocal'), it will clear out the files in the defined directory when that method is called. However, there is nothing that automatically clears out the files again once the test is complete.

如果要在测试完成后清空目录,则有两种选择.

If you want to empty out the directory after your test is complete, you have a couple options.

  1. 您可以在测试结束时再次调用 Storage :: fake('reportslocal').这将运行代码以清除伪造的磁盘.

  1. you can just call Storage::fake('reportslocal') again at the end of your test. That'll run the code to clear out the fake disk.

您可以调用代码以自己手动清除假磁盘:

you can call the code to manually clear out your fake disk yourself:

(new Illuminate\Filesystem\Filesystem)->cleanDirectory(Storage::disk('reportslocal')->path(''))

在这里小心!如果您运行上述命令,但首先忘记伪造磁盘,则将清空实际磁盘.因此,实际上,在测试结束时再次调用 Storage :: fake('reportslocal')会更安全.

Careful here! If you run the above command, but forgot to fake your disk first, you'll empty out your real disk. So, really, you'd be safer just to call Storage::fake('reportslocal') a second time at the end of your test.

这篇关于Laravel使用伪造的存储进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆