【问题标题】:PHPUnit test of Symfony CLI command falling over when accessing 3rd party API访问 3rd 方 API 时 Symfony CLI 命令的 PHPUnit 测试失败
【发布时间】:2018-06-28 03:03:38
【问题描述】:

我的 Symfony 2 应用程序有一个 PHPUnit 测试,该测试在以下行失败:

if ($subscriptions = $subscriptionEngine->getAllSubscriptions($site->getOwner())) {

错误是:

Sorry, we couldn't find the site SITEKEY

我明白这是为什么,因为我的parameters.yml.dist 包含以下内容:

subscriptionengine.site: SITEKEY

我的问题是我应该创建一个测试 API 密钥/站点密钥进行测试,还是根本不进行测试?

测试正在测试调用 Symfony CLI 命令,该命令调用此第三方 API 等。我的方法是错误的,还是像创建仅供 PHPUnit 使用的测试密钥一样简单?

【问题讨论】:

  • 为什么不只是模拟订阅引擎及其响应,这样您就可以运行它而无需访问第 3 方 API?

标签: unit-testing symfony testing phpunit symfony-2.8


【解决方案1】:

以下工作。

$subEngine = $this->getMockBuilder('\AppBundle\Services\SubEngine')
->disableOriginalConstructor()
->getMock();
$subEngine->expects($this->once())->method('getAllSubscriptions');

感谢 Jason Roman 和 scyrma 对代码的上述评论。

【讨论】:

    猜你喜欢
    • 2014-02-04
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 2016-10-21
    • 2013-07-28
    • 2013-06-11
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多