【问题标题】:How do you mock a function call that called multple times with changing parameters in PHPSpec?您如何模拟在 PHPSpec 中更改参数多次调用的函数调用?
【发布时间】:2017-03-02 11:39:40
【问题描述】:

我正在尝试在 PHPSpec 中为一个函数创建一个测试,该函数调用另一个具有不同参数的对象上的函数。到目前为止,我的尝试已经导致了几个不同的错误,所以我将概述我到目前为止的情况。

最近的错误:

- it should find all realm data
  method call:
    - fetch(LeagueOfData\Adapters\Request\RealmRequest:000000001212f67d000000001262e5c6 Object (
      'apiDefaults' => Array &0 (
          'region' => 'euw'
      )
      'format' => null
      'data' => null
      'query' => null
      'where' => Array &0
  ))
  on Double\AdapterInterface\P51 was not expected, expected calls were:
    fetch(exact(Double\RequestInterface\P50:000000001212f607000000001262e5c6 Object (
      'objectProphecy' => Prophecy\Prophecy\ObjectProphecy Object (*Prophecy*)

PHPSpec 文件:

class JsonRealmsSpec extends ObjectBehavior
{
    function let(AdapterInterface $adapter, LoggerInterface $logger, RequestInterface $request)
    {
        // fetch called with multiple request objects but we're not interested in the exact data it returns yet.
        $adapter->fetch($request)->willReturn(['test data']);
        $this->beConstructedWith($adapter, $logger);
    }

    function it_should_find_all_realm_data()
    {
        $this->findAll()->shouldReturnArrayOfRealms();
    }


    function getMatchers()
    {
        return [
            'returnArrayOfRealms' => function($realms) {
                foreach ($realms as $realms) {
                    if (!$realm instanceof Realm) {
                        return false;
                    }
                }
                return true;
            }
        ];
    }
}

以及正在测试的实际功能:

class JsonRealms implements RealmService
{
    const REGIONS = ['euw', 'eune', 'na'];

    private $source;
    private $log;
    private $realms;

    public function __construct(AdapterInterface $adapter, LoggerInterface $log)
    {
        $this->source = $adapter;
        $this->log = $log;
    }

    public function findAll() : array
    {
        $this->realms = [];
        foreach (self::REGIONS as $region) {
            $request = new RealmRequest(['region' => $region]);
            $response = $this->source->fetch($request);
            $this->realms[] = new Realm($realm['cdn'], $realm['v'], $region);
        }
        return $this->realms;
    }
}

我确定我可能遗漏了一些非常明显的东西,但我现在看不到它。

【问题讨论】:

  • 这里的问题不在于您在 SUS 中多次调用该函数(规范下的系统;您的班级)。问题是您在该类中创建它,而 @spec 级别您断言您希望与协作者进行调用(并且它不是相同的对象(类型)并且具有不同的值)。看看我的answer 有类似的问题。
  • 您好,我不确定我是否完全理解您的回答。我多次调用的函数不是我正在测试的。我只是在嘲笑它。我正在测试 findAll() 函数是否返回一个 Realm 对象数组。这是来自一些重构,我将函数从获取单个领域更改为获取所有领域。我自己已经解决了这个问题,并将添加答案。

标签: symfony php-7 phpspec


【解决方案1】:

所以事实证明我遗漏了一些明显的东西,我试图通过一个模拟调用来解决它,而不是每个案例一个:

function let(AdapterInterface $adapter, LoggerInterface $logger) {
    $request = new RealmRequest(['region' => 'euw']);
    $adapter->fetch($request)->willReturn([
        'cdn' => 'http://test.url/euw',
        'v' => '7.4.3'
    ]);
    $request = new RealmRequest(['region' => 'eune']);
    $adapter->fetch($request)->willReturn([
        'cdn' => 'http://test.url/eune',
        'v' => '7.4.3'
    ]);
    $request = new RealmRequest(['region' => 'na']);
    $adapter->fetch($request)->willReturn([
        'cdn' => 'http://test.url/na',
        'v' => '7.4.3'
    ]);
}

这适当地设置了模拟适配器,以便我们可以测试服务是否正确地创建了领域对象。

【讨论】:

    猜你喜欢
    • 2022-11-02
    • 2016-04-18
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 2022-07-26
    相关资源
    最近更新 更多