如何解决单元测试,模拟文件创建使用硬依赖创建
我尝试模拟文件创建但没有成功。该文件是在函数中硬创建的,没有依赖关系,只有文件名作为参数。
有没有办法从 vfs 文件系统覆盖真实的文件系统?我有一个模拟这个类 File
的解决方案,但我更喜欢保留原来的。
测试类:
class FileWorkflow
{
public function moveAttachmentsFiles(string $filename): void
{
$file = new File(sys_get_temp_dir() . '/' . $filename);
return;
}
}
单元测试:
class FileWorkflowTest extends TestCase
{
private FileWorkflow $fileWorkflow;
/** @var ObjectProphecy | File */
private ObjectProphecy $file;
protected function setUp(): void
{
$this->file = $this->prophesize(File::class);
$this->fileWorkflow = new FileWorkflow();
$tmp = vfsStream::newDirectory('/tmp')
->at(vfsStream::setup('/'))
;
$file = vfsStream::newFile('unit-1')
->at($tmp)
;
// file exists in vfs filesystem
}
public function testMoveAttachmentsFiles(/*array $attachments*/): void
{
$this->file
->getUuid()
->shouldBeCalled()
->willReturn('unit-1','unit-2','unit-3');
$this->fileWorkflow->moveAttachmentsFiles([$this->file->reveal()],'');
// Error "file does not exists" because tested function code is not opening file from vfs filesystem.
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。