2017-03-02 48 views
0

我想在PHPSpec中创建一个测试函数,该函数使用不同参数调用另一个对象上的函数。到目前为止,我的尝试导致了几个不同的错误,所以我会概述我到目前为止所做的。您如何模拟一个调用多次使用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; 
    } 
} 

我敢肯定,我可能丢失一些非常明显的事情,但对于我的生活,我现在看不到它。

+0

这里的问题不是你要在SUS中多次调用函数(系统在规范下;你的类)。问题在于你在该类中创建它,而@ spec级别则声明你期望与协作者进行调用(并且它不是同一个对象(类型)并且具有不同的值)。看看我的[回答](http://stackoverflow.com/questions/42307939/how-to-get-property-from-stub-function-argument)一个类似的问题。 – DonCallisto

+0

嗨,我不确定我完全理解你的答案。我多次调用的函数并不是我正在测试的。我只是在嘲笑它。我测试如果findAll()函数返回一个Realm对象数组。这是从一些重构的地方,我已经改变了从获取单个领域的功能,并获取所有的领域。我已经自己解决了这个问题,并会增加答案。 – Acaeris

回答

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' 
    ]); 
} 

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

相关问题