假装Laravel存储磁盘的例外
您好,我试图失败以模拟异常或问题,目的是测试处理该错误的代码,测试快乐案例,成功上传它很容易,但是我找不到有关它们的例外情况,
这是段落在所讨论的代码中,只需将文件上传到存储磁盘,
private function code_that_runs_on_the_app(string $file, string $filename)
{
$disk = Storage::disk('a_disk_used_for_this');
if ($disk->putFileAs('/', $file, $filename)) {
unlink($file);
} else {
// Handle the error case, like some write permission error
}
}
这就是“失败”的测试,因为它实际上上传了文件,我需要在putfileas上伪造错误,或者可能是一个例外或无效响应
public function it_tests_some_part_of_my_code()
{
$disk = Storage::fake('a_disk_used_for_this');
// Here comes some code that triggers the code needed
// Because it supposedly failed this should be empty
$this->assertEmpty($disk->allFiles());
}
。文档发现我可以使用类似的东西
Storage::shouldReceive('putFileAs')->once()->andThrow(new \Exception('Testing'));
$disk = Storage::fake('a_disk_used_for_this');
...
,但即使在那一步中也会失败。
您知道一种伪造的方法,例如超时错误,连接错误,权限错误等吗?
提前致谢
Hello I'm trying unsuccessfully to emulate an exception or a problem with the purpose of testing the code that handles that error, testing the happy case, successful upload it's easy but the exception cases I couldn't find information about them
This is a snippet of the code in question, just uploading a file to a storage disk
private function code_that_runs_on_the_app(string $file, string $filename)
{
$disk = Storage::disk('a_disk_used_for_this');
if ($disk->putFileAs('/', $file, $filename)) {
unlink($file);
} else {
// Handle the error case, like some write permission error
}
}
This is the test that "fails" because it actually upload the file, I need to fake an error on the putFileAs could be an exception or a null response
public function it_tests_some_part_of_my_code()
{
$disk = Storage::fake('a_disk_used_for_this');
// Here comes some code that triggers the code needed
// Because it supposedly failed this should be empty
$this->assertEmpty($disk->allFiles());
}
Reading about that on the documentation found that I could use something like
Storage::shouldReceive('putFileAs')->once()->andThrow(new \Exception('Testing'));
$disk = Storage::fake('a_disk_used_for_this');
...
But it fails even on that step.
Do you know a way to fake like a timeout error, a connection error, permissions error, etc?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定什么对您不起作用,但我认为这会对您有所帮助:
I am not sure what is not working for you, but I think this will help you:
要测试超时错误,您可以受到。就个人而言,没有尝试过,但是您可以尝试将时间限制设置为非常低的值。
要测试连接错误,试图复制到某些随机不存在的服务器可能会起作用。只需将
设置在何处将
参数复制到https://a/
以测试许可,您可以在测试中使用
mkdir()然后使用
CHMOD
或chown
设置其权限。需要明确的是,我没有尝试过任何这些,但是他们应该努力提出错误/例外。
to test a timeout error, you can get inspired by PHP copy/fopen etc. times out. Personally, haven't tried, but you can try out setting the time limit to very low value.
To test a connection error, trying to copy to some random-not-existing server might work. Simply set the
where to copy
parameter tohttps://a/
To test permission, you can create a directory within a test using
mkdir()
and then set its permissions usingchmod
orchown
.To be clear, I have not tried any of these, but they should work to throw the errors/exceptions.