无法读取运行NPM和PACT引发的未定义属性

时间:2018-11-26 15:42:39

标签: javascript node.js pact

我正在尝试使用PACT研讨会示例提供一些替代数据。

这可能是更多关于Javascript / Node的问题,但是作为一个新手,我很困惑。

给出以下文件的Consumer.spec.js文件:

const chai = require('chai');
const nock = require('nock');
const chaiAsPromised = require('chai-as-promised');
const expect = chai.expect;
const API_PORT = process.env.API_PORT || 9123;
chai.use(chaiAsPromised);

const API_HOST = `http://localhost:${API_PORT}`;

describe('Consumer', () => {
  describe('when a call to the Provider is made', () => {
    const clothingStatus = 'hello';
    const {emailClothingOfferStatus} = require('../client');

    it('can process the HTML payload from the provider', () => {
      nock(API_HOST)
        .get('/provider')
        .query({validPermStatus:'hello'})
        .reply(200, {
          test:'NO',
          validPermStatus: clothingStatus,
          count: 1000,
        });

      const response = emailClothingOfferStatus(clothingStatus);

      return expect(response.body.clothingStatus).to.eventually.equal('hello')
    })
  })
});

和以下客户端.js文件:

const request = require('superagent');
const API_HOST = process.env.API_HOST || 'http://localhost';
const API_PORT = process.env.API_PORT || 9123;
const API_ENDPOINT = `${API_HOST}:${API_PORT}`;

// Fetch provider data
const emailClothingOfferStatus = emailPermChoice => {
  let withEmailClothing = {};
  const emailClothingGrantedRegex = 'hello';

  if(emailPermChoice){
    console.log(emailPermChoice);
    withEmailClothing = {validPermStatus: emailPermChoice}
  }

  return request
    .get(`${API_ENDPOINT}/provider`)
    .query(withEmailClothing)
    .then(
      res => {
        if (res.body.validPermStatus.match(emailClothingGrantedRegex)) {
          return {
            clothingStatus: (res.body.validPermStatus),
          }
        } else {
          throw new Error('Could not verify email clothing offer status')
        }
      },
      err => {
        throw new Error(`Error from response: ${err.body}`)
      }
    )
};

module.exports = {
  emailClothingOfferStatus,
};

并且我的package.json脚本中包含以下内容:

"test:consumer": "./node_modules/.bin/mocha --timeout 150000 pact/consumer/test/consumer.spec.js",

当我运行npm运行test:consumer时,我得到:

1) Consumer
       when a call to the Provider is made
         can process the HTML payload from the provider:
     TypeError: Cannot read property 'clothingStatus' of undefined
      at Context.it (pact/consumer/test/consumer.spec.js:29:35)

我确定这很明显,但是任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

您的函数@NgModule({ declarations: [...routedComponents], imports: [CommonModule, RouterModule, CountryRoutingModule], }) export class CountriesModule {} 返回emailClothingOfferStatus,这是一个承诺,而不是实际的响应。

因此,response.then()response.body

您应该能够像这样测试结果:

undefined

答案 1 :(得分:0)

两个问题对我来说很突出:

  1. 上面的测试是普通的单元测试,旨在显示单元测试如何解决合同问题,并引导您了解Pact为何有用(如果尚不清楚的话)。简而言之,它根本不是一个Pact测试-我可以断定,因为它使用的是Nock,这意味着预期的请求将永远不会达到Pact。我还可以告诉您,因为Pact软件包似乎没有被导入。您要使用此文件https://github.com/DiUS/pact-workshop-js/blob/master/consumer/test/consumerPact.spec.js
  2. 进行建模
  3. response值是一个Promise,这意味着您不能执行return expect(response.body.clothingStatus).to.eventually.equal('hello'),因为响应是一个承诺,因此body将是undefined和{{1} }并非该属性。 chai 最终 API对于这种测试很有用,但是据我了解,它必须直接与Promise配合使用-您可以执行clothingStatus,然后chai可以开始工作。< / li>