我需要在 debug = false 时测试 storeDocument 函数,该函数将调用 createStorageDocument。但是对于我下面的测试代码给出了一个错误'TypeError: Attempted to wrap undefined property createStorageDocument as function'
我究竟做错了什么 ?
而且我更喜欢在 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();
});
});
});