【 callable-fake】虚构你的可调用函数以加速测试-php教程

资源魔 818 0
Callable fake 是 Tim Macdonald 的一个 PHP 测试适用顺序,它 “容许您捏造、捕捉以及断言对可挪用 / 闭包的挪用”。正在某些状况下,此包能够协助正在测试中容许开发职员通报一个 callable。

它有一个受 Laravel 虚拟启示的 API,以下所示:

// Before, you might collect callables to assert later...
public function testEachLoopsOverAllDependencies(): void
{
    // arrange
    $received = [];
    $expected = factory(Dependency::class)->times(2)->create();
    $repo = $this->app[DependencyRepository::class];
    // act
    $repo->each(function (Dependency $dependency) use (&$received): void {
        $received[] = $dependency;
    });
    // assert
    $this->assertCount(2, $received);
    $this->assertTrue($expected[0]->is($received[0]));
    $this->assertTrue($expected[1]->is($received[1]));
}

应用此软件包,您能够应用相似如下内容的内容:

public function testEachLoopsOverAllDependencies(): void
{
    // arrange
    $callable = new CallableFake();
    $expected = factory(Dependency::class)->times(2)->create();
    $repo = $this->app[DependencyRepository::class];
    // act
    $repo->each($callable);
    // assert
    $callable->assertTimesInvoked(2);
    $callable->assertCalled(function (Depedency $dependency) use ($expected): bool {
        return $dependency->is($expected[0]);
    });
    $callable->assertCalled(function (Dependency $dependency) use ($expected): bool {
        return $dependency->is($expected[1]);
    });
}

该包提供了诸如 assertCalled、assertNotCalled、assertInvoked 等断言。无关具体信息以及示例,请务必查看名目自述文件中的可用 assertions 的完好列表。

你能够正在 GitHub 上理解更多对于此软件包的信息,猎取完好的装置阐明,并正在 timacdonald/callable-fake 上查看源代码。

以上就是【 callable-fake】虚拟你的可挪用函数以减速测试的具体内容,更多请存眷资源魔其它相干文章!

标签: php开发教程 php开发资料 php开发自学 Callable fake

抱歉,评论功能暂时关闭!