错误:在尝试在pact javascript中实现多个交互时多次调用done()

时间:2017-12-20 23:09:42

标签: javascript node.js mocha pact

  

我正在尝试使用一个pact服务器服务器创建多个交互,但是当我运行代码时,我收到此错误:Error: done() called multiple times。必须可以只用一个pact服务器创建多个交互,但我无法弄清楚如何。我已阅读此https://github.com/pact-foundation/pact-js文档,其中指出可以仅与一个服务器创建多个交互。

const interaction1 = {
    state: 'User exists',
    uponReceiving: 'a request containing users preferences',
    withRequest: {
        method: 'GET',
        path: '/members/profile',
        headers: {
            'Host': "localhost:3000",
            'Accept-Encoding': "gzip, deflate",
            'User-Agent': "node-superagent/1.8.5",
            'Authorization': ``Bearer ${ACCESS_TOKEN`}`,
            'Connection': "close",
            'Https': "on",
            'Version': "HTTP/1.1"
       }
  },
    willRespondWith: {
        status: 200,
        headers: {
            'Content-Type': 'application/json'
        },
        body: RESPONSE_BODY1
  }
};


const interaction2 = {
    state: 'User exists',
    uponReceiving: 'a request containing user profile',
    withRequest: {
        method: 'GET',
        path: '/member/preferences',
        headers: {
            'Host': "localhost:3000",
            'Accept-Encoding': "gzip, deflate",
            'User-Agent': "node-superagent/1.8.5",
            'Authorization': ``Bearer ${ACCESS_TOKEN}``,
            'Connection': "close",
            'Https': "on",
            'Version': "HTTP/1.1"
        }
   },
   willRespondWith: {
       status: 200,
       headers: {
          'Content-Type': 'application/json'
      },
      body: RESPONSE_BODY2
   }
};


describe('Integration', () => {
    before((done) => {
        provider.setup()
           .then(() => {
               // multiple interactions produce the error.
               provider.addInteraction(interaction1)
                   .then(() => done());
               provider.addInteraction(interaction2)
                   .then(() => done()); 
       })
    })
})`

1 个答案:

答案 0 :(得分:2)

完成后,您只需拨打Promise.all即可。因此,您可以在另一个之后运行一个调用,如下所示:

Promise.all([provider.addInteraction(interaction1), provider.addInteraction(interaction2)])
    .then(() => done()); 

或者您可以使用iOS 11同时运行它们,并等待两者完成:

iOS 10