0

createStorageDocument を呼び出す debug = false の場合、storeDocument 関数をテストする必要があります。しかし、次のテスト コードでは、「TypeError: 未定義のプロパティ createStorageDocument を関数としてラップしようとしました」というエラーが表示されます。

私は何を間違っていますか?

そして、createStorageDocument自体ではなく、createStorageDocument関数内でスタブフェッチするソリューションを好むでしょう。誰かがその方法を説明できますか? 私はモカと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();
      });
  });
});

4

1 に答える 1