【发布时间】:2020-09-22 13:19:36
【问题描述】:
我有这个调用azure table storage的函数:
const azure = require('azure-storage');
...
function getProductMetadata(sku, locale) {
const retryPolicy = new azure.ExponentialRetryPolicyFilter(0, 200, 200, 600);
const tableSvc = azure.createTableServiceWithSas(HOST, `?${AUTH}`).withFilter(retryPolicy);
const payloadFormat = azure.TableUtilities.PayloadFormat.MINIMAL_METADATA;
const partition = `${sku}-description`;
return new Promise<PDAPIProduct.Tabledata>((resolve) => {
tableSvc.retrieveEntity('ProductMetadata', partition, locale, { payloadFormat }, (error, result, response) => {
if (get(response, 'statusCode') === 404) {
resolve(null);
} else {
resolve(response.body);
}
});
});
}
这个功能的玩笑测试:
const azure = require('azure-storage');
jest.mock('azure-storage');
const tableServiceMock = {
retrieveEntity: jest.fn,
withFilter: jest.fn,
}
azure.createTableServiceWithSas.mockImplementationOnce(() => tableServiceMock);
afterEach(jest.clearAllMocks);
describe('Product module', () => {
describe('getProductMetadata', () => {
test('getProductMetadata via azure storage SDK', async () => {
const retValue = await pdApiProductService.getProductMetadata('213123123', 'en-CA');
///expect( something)
});
});
});
但是当我运行时,它会出错:
TypeError: tableSvc.retrieveEntity is not a function
我做错了什么?
顺便说一句,我的测试基于这篇文章: https://medium.com/@lakshaykaushik2506/azure-functions-unit-testing-mocking-azure-storage-npm-module-with-jest-34316fac6a69
【问题讨论】:
-
当我在函数中 console.log tableSvc 时,在测试运行下我得到这个: tableSvc: function mockConstructor() { return fn.apply(this, arguments);所以它似乎没有得到完整的模拟实现:( .. 不知道如何解决这个问题。
标签: javascript jestjs azure-storage