2017-06-02 34 views
3

我们一直在试图测试从另一个微服务正在使用的微服务(称为GET/contacts)暴露的API。为多个响应集测试相同的API

为了避免集成测试,我们创建其中消费者微服务创建协定消费者驱动的合同测试,并公布他们从其中生成器将分别验证协议的经纪人。

我们用Pact IO来达到这个目标,到目前为止它已经相当不错了。

现在,我们正在尝试做详尽的测试,我们希望看到怎样一个空列表是从GET /触点返回时面临的问题。

问题是:在添加交互时,我们可以使用提供者状态,但是我们找不到一种方法来区分编写用于从GET/contacts中获取联系人列表的一次测试和在另一个测试中获得空列表的测试。

这就是我们如何在消费者的microService创建协议测试:

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 
      } 
      }) 
     }) 

我们能不能找到一种方法,在我们的测试中,这些interations区分! :(

任何帮助,将不胜感激

感谢

回答

1

假设你正在使用类似摩卡,你应该将这些互动分成单独的测试 - 如:拨打addInteraction每个describe块的内容有关,您正在运行的测试案例(在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()