无法读取从 运行 NPM 和 PACT 抛出的 属性 未定义

Cannot read property of undefined thrown from running NPM and 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)

我确定这很明显,但有人可以帮忙吗?

您的功能 emailClothingOfferStatus returns response.then() 这是一个承诺,而不是实际的响应。

因此response.bodyundefined

您应该可以像这样测试结果:

const response = emailClothingOfferStatus(clothingStatus);
response.then((res) => {
  expect(res.body.clothingStatus).to.eventually.equal('hello')
})

有两件事对我来说是一个突出的问题:

  1. 上面的测试是一个正常的单元测试,旨在展示单元测试如何无法捕获契约问题,并引导您了解 Pact 为何有用(如果这不是'不清楚)。简而言之,这根本不是 Pact 测试——我可以说是因为它使用的是 Nock,这意味着预期的请求永远不会到达 Pact。我还可以判断,因为 Pact 包似乎没有被导入。您想从此文件建模 https://github.com/DiUS/pact-workshop-js/blob/master/consumer/test/consumerPact.spec.js
  2. response 值是一个 Promise,这意味着你不能做 return expect(response.body.clothingStatus).to.eventually.equal('hello') 因为 response 是一个 promise,所以 body 将是 undefinedclothingStatus 不是那个的 属性。 chai eventually API 对这种测试很有用,但据我所知,它必须直接与 Promise 一起工作——你可以做 expect(response).to... 然后小柴可以上班了