我需要在debug = false时测试storeDocument函数,这将调用createStorageDocument。但是对于我的以下测试代码给出错误'TypeError:尝试将未定义的属性createStorageDocument包装为函数'
我做错了什么?
而且我更喜欢在createStorageDocument函数中使用存根获取的解决方案,而不是createStorageDocument本身,有人可以解释怎么做吗?我是mocha和node.js的新手
import fetch from 'node-fetch';
import documentStorageResponseMock from '../../../test/mock/documentStorageResponseMock';
const storageGatewayUrl = 'url';
const storageGatewayApiKey = 'key';
/**
* Merge storage id into booking response.
*
* @param booking
* @param documentId
* @returns {*}
*/
function mergeDocumentId(booking, documentId) {
const mergedBooking = booking;
mergedBooking.successfulBooking.documentId = documentId.id;
return mergedBooking;
}
/**
* @param labelledBooking
*/
function createStorageDocument(labelledBooking) {
fetch(storageGatewayUrl,
{
method: 'POST',
body: {
document: labelledBooking,
},
headers: {
ContentType: 'application/json',
'x-api-key': storageGatewayApiKey,
},
})
.then((response) => {
if (response.ok === false) {
throw new Error('Failed to create the document!');
} else {
return response.json();
}
}).catch((err) => {
throw err;
});
}
/**
* Save booking response and add the document id to the response.
*
* @param labelledBooking
* @param debug
* @param callback
*/
export default function storeDocument(labelledBooking, debug = false, callback) {
if (debug) {
callback(
null,
mergeDocumentId(labelledBooking, documentStorageResponseMock())
);
return;
}
callback(null, mergeDocumentId(labelledBooking, createStorageDocument(labelledBooking)));
}
import storeDocument from '../../../lib/documents/storeDocument';
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
chai.use(require('dirty-chai'));
chai.use(require('chai-fuzzy'));
describe('merge document storage id', function () {
before(function(callback) {
sinon.stub(storeDocument, 'createStorageDocument', function (params, callback) {
return ({id: '1087449a-1248-4430-9bcb-5a61b2766020'})
});
});
it('it creates and appends document id to booking when storage gateway is provided ', function(done) {
storeDocument({
integrationId: 'testing',
successfulBooking: {
integrationServiceId: 'service-id',
bookingReference: '#ref',
shippingTaxInclusiveCharge: { amount: 10, currency: 'EUR' },
pricedAt: '2016-05-20T15:00:00Z',
documentation: {
labelDocument: 'ero32ukj32hr3h'
}
}
},
false,
(error, booking) => {
expect(booking.successfulBooking.bookingReference === '#ref').to.be.true;
expect(booking.successfulBooking.documentation !== undefined).to.be.true;
expect(booking.successfulBooking.documentId !== '').to.be.true;
done();
});
});
});
答案 0 :(得分:1)
部分问题是createStorageDocument
返回Promise
,而不是标量值。首先,我重新设计storeDocument
。
/**
* Save booking response and add the document id to the response.
*
* @param labelledBooking
* @param {Function} createStorageDocument
* @param {Function} callback
*/
export default function storeDocument(labelledBooking,
createStorageDocument,
callback) {
createStorageDocument(labelledBooking)
.then(function (documentId) {
callback(null, mergeDocumentId(labelledBooking, documentId));
})
.catch(callback);
}
好的,这里发生的是我们使用依赖注入来注入将存储对象的对象,我们正在正确处理Promise。
然后你要修复createStorageDocument
:
export function createStorageDocument(labelledBooking) {
return fetch(storageGatewayUrl, {
method: 'POST',
body: {
document: labelledBooking,
},
headers: {
ContentType: 'application/json',
'x-api-key': storageGatewayApiKey,
},
})
.then((response) => {
if (response.ok === false) {
throw new Error('Failed to create the document!');
}
return response.json();
});
}
这里我在return
之前插入了fetch
,你不再需要捕捉了(它无论如何都不会起作用)。我已将其导出,因此您必须在实际实施中使用它。
好的,现在进行测试。你不需要柴 - 它只是复杂的东西。谨慎使用Sinon,只有当你更了解Node时。您的测试可能会变成这样:
it('it creates and appends document id to booking when storage gateway is provided ', function(done) {
let createStorageDocumentStub = function (labelledBooking) {
return Promise.resolve('documentId???')
}
storeDocument({
integrationId: 'testing',
successfulBooking: {
integrationServiceId: 'service-id',
bookingReference: '#ref',
shippingTaxInclusiveCharge: { amount: 10, currency: 'EUR' },
pricedAt: '2016-05-20T15:00:00Z',
documentation: {
labelDocument: 'ero32ukj32hr3h'
}
}
},
createStorageDocumentStub,
(error, booking) => {
if (error) {
return done(error);
}
assert(booking.successfulBooking.bookingReference === '#ref'));
assert(booking.successfulBooking.documentation !== void 0);
assert(booking.successfulBooking.documentId !== '');
done();
});
});
我所做的是为应该存储文档的函数创建一个存根(不是模拟,这是一个不同的东西),并且我用简单的旧assert
替换了你的断言包含在Node中。并且不要忘记在测试中处理错误(他们仍然会咬你)。
说实话,如果storeDocument
也返回Promise而不是使用丑陋的回调函数会更好。
我意识到这可能需要付出很多。