• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-01-20) 20次浏览 已收录 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->asser<strong>)本文来(源gaodai#ma#com搞@@代~&码*网2</strong><pre>搞代gaodaima码

tTrue($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】虚构你的可调用函数以加速测试的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:【 callable-fake】虚构你的可调用函数以加速测试
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址