最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 8浏览 0评论

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

我试图只与一个pact服务器服务器创建多个交互,但是当我运行代码时,我收到此错误:Error: done() called multiple times。必须可以只用一个pact服务器创建多个交互,但我无法弄清楚如何。我已经阅读了这篇,其中指出可以与一个服务器创建多个交互。

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()); 
       })
    })
})`
回答如下:

完成后你只需要调用done。因此,您可以在另一个之后运行一个调用,如下所示:

provider.addInteraction(interaction1)
    .then(() => provider.addInteraction(interaction2));
    .then(() => done());

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

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

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

我试图只与一个pact服务器服务器创建多个交互,但是当我运行代码时,我收到此错误:Error: done() called multiple times。必须可以只用一个pact服务器创建多个交互,但我无法弄清楚如何。我已经阅读了这篇,其中指出可以与一个服务器创建多个交互。

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()); 
       })
    })
})`
回答如下:

完成后你只需要调用done。因此,您可以在另一个之后运行一个调用,如下所示:

provider.addInteraction(interaction1)
    .then(() => provider.addInteraction(interaction2));
    .then(() => done());

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

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

评论列表 (0)

  1. 暂无评论