我正在尝试使用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)
我确定这很明显,但是任何人都可以帮忙吗?
答案 0 :(得分:0)
您的函数@NgModule({
declarations: [...routedComponents],
imports: [CommonModule, RouterModule, CountryRoutingModule],
})
export class CountriesModule {}
返回emailClothingOfferStatus
,这是一个承诺,而不是实际的响应。
因此,response.then()
是response.body
。
您应该能够像这样测试结果:
undefined
答案 1 :(得分:0)
两个问题对我来说很突出:
response
值是一个Promise,这意味着您不能执行return expect(response.body.clothingStatus).to.eventually.equal('hello')
,因为响应是一个承诺,因此body
将是undefined
和{{1} }并非该属性。 chai 最终 API对于这种测试很有用,但是据我了解,它必须直接与Promise配合使用-您可以执行clothingStatus
,然后chai可以开始工作。< / li>