2016-05-07 5 views
3

私は、テストしたいjavascript(動的フォーム)がまともな量のアプリケーションを開発中です。私はPHPUnit Mink + ZombieJSを使用することに決めました。Minkでテストするときのサービスを批判しますか?

外部APIと通信するいくつかのサービスを模擬したいのですが、Minkのようなテストフレームワークを使用してこれをどうやって行うことができるかについての情報はありません。 SymfonyのWebTestCaseを使用していた場合は、

TestDoubleBundleのバンドルを使用しますが、Minkのような実際のリクエストでは機能しません。

他にも解決策がありますか?私のAPIサービスの "テスト"バージョンを作成し、実際のサービスの代わりにそのサービスをロードするようにテスト環境を設定するのが最善の方法だろうかと思います。テストサービスは、あらかじめ決められた応答でさまざまなAPIメソッドに応答します。大きな欠点は、それほど柔軟性がないということです。APIメソッドから期待される応答を動的に構成することはできません。

+0

あなたはbehatを使用していますか? – BentCoder

+0

いいえ、ちょうどphpunit-mink – Brian

答えて

0

TestDoubleBundleの仕組みを実際に試してみるのに少し苦労しましたが、実際は非常に簡単です。私はそれがあなたに役立つことを望む実際の例をあなたに示します。

ケーススタディ:実際のところ、http://api.postcodes.io/postcodes/{postcode} APIで特定の郵便番号の詳細を取得してください。存在しない郵便番号を指定すると、nullが返されます。

以下の例は、完全版の場合はMocking external APIs with behat in symfonyに移動する必要があるため、縮小版です。

FULL例

services.yml

services: 
    app.service.postcode: 
     class: AppBundle\Service\PostcodeService 
     arguments: 
      - "@app.util.guzzle" 
      - "%postcodes_api%" 

    app.util.guzzle: 
     class: GuzzleHttp\Client 
     tags: 
      - { name: test_double } 

PostcodeService

class PostcodeService 
{ 
    private $client; 
    private $apiUri; 

    public function __construct(
     ClientInterface $client, 
     $apiUri 
    ) { 
     $this->client = $client; 
     $this->apiUri = $apiUri; 
    } 

    public function get($postcode) 
    { 
     return $this->getDetails($postcode); 
    } 

    private function getDetails($postcode) 
    { 
     $details = null; 

     try { 
      /** @var Response $response */ 
      $response = $this->client->request(Request::METHOD_GET, $this->apiUri.$postcode); 
      /** @var Stream $body */ 
      $body = $response->getBody(); 
      $details = $body->getContents(); 
     } catch (ClientException $e) { 
     } 

     return $details; 
    } 
} 

FeatureContext

/** 
* @param string $postcode 
* 
* @Given /^the Postcode API is available for "([^"]*)"$/ 
*/ 
public function thePostcodeApiIsAvailableFor($postcode) 
{ 
    $postcodesApi = $this->kernel->getContainer()->getParameter('postcodes_api').$postcode; 

    $response = new Response(200, [], '{"result":{"latitude":0.12345678,"longitude":-0.1234567}}'); 

    $this->kernel 
     ->getContainer() 
     ->get('app.util.guzzle.prophecy') 
     ->request(Request::METHOD_GET, $postcodesApi) 
     ->willReturn($response); 
} 

ガーキン

Scenario: I get full result for existent postcode. 
    Given the Postcode API is available for "DUMMY POSTCODE" 
    When I send a "GET" request to "/postcodes/DUMMY POSTCODE" 
    Then the response status code should be 200 
    And the response should contain json: 
    """ 
    {"result":{"latitude":0.12345678,"longitude":-0.1234567}} 
    """ 
+0

私はこれらのステップに従った。私のコントローラーのサービスが正しいタイプのdouble(ProphecySubjectInterfaceを実装しています)でも、メソッドの予言を失うようです。したがって、メソッド呼び出しは、スタブが何を言っても、常にnullを返します。 '$ service-> getProphecy() - > getMethodProphecies()'は私には何も与えませんでしたが、私が設定したBehatコンテキストでは正しいカウントを示しました。何か案は? – afilina

関連する問題