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 上查看源代码。

更多相关文章

  1. Python社区软件包概况分析
  2. 如何部署Bower安装的软件包?
  3. 在单个测试中断言多个条件,还是分成多个测试? [重复]
  4. Ubuntu 安装XAMPP集成环境软件包 与 运行WordPress 的简单方法
  5. linux mint 19安装 kvm 软件包
  6. 设置yum软件库轻松解决软件包安装问题
  7. SQL Server 执行计划操作符详解(1)——断言(Assert)
  8. Maven编译提示:软件包不存在

随机推荐

  1. android:layout_gravity与android:gravity
  2. Adb移植(一)简单分析
  3. android 几个常用命令
  4. Android——设置固定横竖屏
  5. Android 下载文件及写入SD卡
  6. Android官方入门文档[2]运行你的应用程序
  7. LinearLayout和RelativeLayout的比较
  8. Android 4编程入门经典—开发智能手机与
  9. Android编译过程详解
  10. [Android 新特性] 改进明显 Android 4.4