2017-03-02 5 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(system under spec;あなたのクラス)で関数を何度も呼び出すことではないということです。問題はあなたがそのクラスの中でそれを作成しているのに対し、@ specのレベルでは、あなたが協力者との呼び出しを期待していることです(同じオブジェクト(タイプ)ではなく、同じ値ではありません)。同様の質問については私の[answer](http://stackoverflow.com/questions/42307939/how-to-get-property-from-stub-function-argument)を見てください。 – DonCallisto

+0

こんにちは、私はあなたの答えを完全に理解しているとは確信していません。私が複数回呼び出す関数は、私がテストしている関数ではありません。私は単にそれを嘲笑しているだけです。 findAll()関数がRealmオブジェクトの配列を返すかどうかをテストしています。これは、私が関数を単一のレルムをフェッチしてからすべてをフェッチするように変更したリファクタリングのものです。私は自分自身で問題を解決し、答えを追加します。 – Acaeris

答えて

1

だから、それぞれのケースのために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オブジェクトを正しく作成しているかどうかテストできます。

関連する問題