【问题标题】:Testing implementation of Symfony 2 Finder component with PhpSpec使用 PhpSpec 测试 Symfony 2 Finder 组件的实现
【发布时间】:2016-03-03 11:43:13
【问题描述】:

我在测试使用 Filesystem 组件访问 Symfony 2 应用程序中的文件系统,然后使用 Finder 组件访问目录中的文件列表时收到以下错误。

Call to undefined method Prophecy\Prophecy\MethodProphecy::in()

这是我正在测试的方法的 sn-p:

$finder = new Finder();

if ($filesystem->exists($imageImportPath)) {
   $finder->files()->in($imageImportPath);
   foreach ($finder as $image) {         
      // ...do some stuff in here with uploading images and creating an entity...
      $this->entityManager->persist($entity);
      $this->entityManager->flush($entity);
   }    
}

这是我对助手类的规范:

function it_imports_image_assets(
    Filesystem $filesystem,
    EntityManager $entityManager,
    Finder $finder
) {
    $imageImportPath = '/var/www/crmpicco/app/files/images/rfc-1872/';
    $filesystem->exists($imageImportPath)->willReturn(true);

    $finder->files()->in($imageImportPath)->shouldHaveCount(2);

    $this->importImageAssets($imageImportPath)->shouldReturn([]);
}

【问题讨论】:

    标签: php symfony tdd php-5.6 phpspec


    【解决方案1】:

    您不想使用真实文件测试您的方法(查看我假设您想要执行的代码),测试应该在没有它的情况下工作。您需要对代码进行单元测试,因此您可以将Finder 找到的文件路径伪装成您喜欢的任何文件路径,代码不应依赖某些第三方文件才能通过测试。

    您将希望在 $finder->files() 方法上返回 Finder 对象,见下文:

    $finder->files()->shouldBeCalled()->willReturn($finder);
    $finder->in($imageImportPath)->willReturn($finder);
    
    $finder->getIterator()->willReturn(new \ArrayIterator([
        $file1->getWrappedObject(),
        $file2->getWrappedObject(),
    ]));
    

    例子:

    use Symfony\Component\Finder\SplFileInfo;
    //..
    
    function it_imports_image_assets(
        Filesystem $filesystem,
        EntityManager $entityManager,
        Finder $finder,
        SplFileInfo $file1,
        SplFileInfo $file2
    ) {
        $imageImportPath = '/var/www/crmpicco/app/files/images/rfc-1872/';
        $filesystem->exists($imageImportPath)->willReturn(true);
        $finder->files()->willReturn($finder);
        $finder->in($imageImportPath)->willReturn($finder);
    
        $finder->getIterator()->willReturn(new \ArrayIterator([
            $file1->getWrappedObject(),
            $file2->getWrappedObject(),
        ]));
    
        $file1->getPathname()->willReturn($imageImportPath.'file1.txt');
        $file2->getPathname()->willReturn($imageImportPath.'file1.txt');
    
        //...
    
        $this->importImageAssets($imageImportPath)->shouldReturn([]);
    }
    

    【讨论】:

    • 感谢您的回复,但这对我不起作用。我收到以下错误:some predictions failed: Double\Symfony\Component\Finder\Finder\P17: No calls have been made that match: Double\Symfony\Component\Finder\Finder\P17->files() but expected at least one.
    • 尝试从$finder->files()->shouldBeCalled()->willReturn($finder);中删除shouldBeCalled()
    • 谢谢!你能更新你的答案,我很乐意接受吗?
    • 已更新。我很高兴它有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多