2017-06-02 4 views
3

別のマイクロサービスで使用されているマイクロサービス(GET /連絡先など)から公開されたAPIをテストしようとしています。複数の応答セットで同じAPIをテストする

インテグレーションテストを避けるため、コンシューマーマイクサービスが協定を作成し、プロデューサーが協定を個別に確認するブローカーに公表した消費者主導の契約テストを作成しました。

これを達成するためにPact IOを使用しました。

ここでは、GET/contactsから空のリストが返される様子を見たい場合に、徹底的なテストを行う際に問題に直面しています。

問題は、対話を追加する際にProvider Statesを使用することができますが、GET /連絡先から一度連絡先のリストを取得し、別のテストで空のリストを取得するテストを区別する方法が見つかりませんでした。私たちは、私たちのテストでは、これらのinterationsを区別するための方法を見つけることができません

mockServer.start() 
     .then(() => { 
      provider = pact({ 
      // config 
      }) 
      return provider.addInteraction({ 
      state: 'Get all contacts', 
      uponReceiving: 'Get all contacts', 
      withRequest: { 
       method: 'GET', 
       path: '/contacts', 
       headers: { 
       Accept: 'application/json; charset=utf-8' 
       } 
      }, 
      willRespondWith: { 
       status: 200, 
       body: //list of all contacts 
      } 
      }) 
     .then(() => { 
      return provider.addInteraction({ 
      state: 'Get empty list of contacts', 
      uponReceiving: 'Get empty list of contacts', 
      withRequest: { 
       method: 'GET', 
       path: '/contacts', 
       headers: { 
       Accept: 'application/json; charset=utf-8' 
       } 
      }, 
      willRespondWith: { 
       status: 200, 
       body: [] // empty list 
      } 
      }) 
     }) 

これは、我々消費者microserviceにおける協定のテストを作成する方法です! :(

任意の助けいただければ幸い

おかげ

答えて

1

あなたはモカのようなものを使用していると仮定し、あなたは個々のテストにこれらの相互作用を分ける必要があります - !。例えばへの文脈だ各describeブロックでaddInteractionを呼び出しをあなたが実行しているテストケース(おそらくbeforeであなたのテストをより明確にするために)

あなたの一般的な構造は、以下の(擬似コード)を次のようになります。

context("Contacts exist") 
    describe("call some API") 
    before() 
     provider.addInteraction(interactionWithContacts) 

    it("Returns a list of contact objects") 
     # your test code here 
     # Verify - this will also clear interactions so 
     # your next test won't conflict 
     provider.verify() 

context("No contacts exist") 
    describe("call some API") 
    before() 
     provider.addInteraction(interactionWithNoContacts) 

    it("Returns an empty list of contacts") 
     # your test code here 
     # Verify - this will also clear interactions so 
     # your next test won't conflict 
     provider.verify()  
関連する問題