所以我做了大量的阅读和搜索,但似乎无法弄清楚这一点。
我有一个typescript类,如下所示
import * as Shopify from 'shopify-api-node';
export default class ShopifyService {
private readonly shopify: Shopify;
constructor(
config: Shopify.IPublicShopifyConfig | Shopify.IPrivateShopifyConfig,
) {
this.shopify = new Shopify({ ...config, apiVersion: SHOPIFY_API_VERSION });
}
public async getCurrentBulkOperation(): Promise<
ICurrentBulkOperation | undefined
> {
const response = await this.shopify.graphql(bulkOperationStatusQuery);
return response?.currentBulkOperation;
}
}这个类只由多个函数组成,这些函数进行graphQL调用(this.shopify.graphql)并返回结果。
我还有另一个名为polly.ts的文件
export const pollyWolly = {
async cons(....) {
const shopify = new ShopifyService({ accessToken, shopName });
const m = await shopify.getCurrentBulkOperation();
// do stuff with m. Ex: get f from m, send f to some queue, etc etc.
}
}我正在为pollyWolly编写测试。例如,当cons运行并且shopify.getCurrentBulkOperation返回null时,测试结果可能是嘿,然后确保消息被发送到某个队列,等等。
我想模拟shopify.getBulkOperation -->我不想对shopify进行实际的API调用...我想用不同的价值观来嘲笑。例如,在一个测试用例中,我想假设getBulkOp返回null。在另一个例子中,我想想象一下getBulkOperation返回了一个具有特定字段等的对象。
我读了很多stackoverflow的帖子,很多文章,但似乎不太理解。
感谢你的帮助。
发布于 2021-03-25 10:03:50
我们可以使用Mock using module factory parameter模拟es6类,并使用mockFn.mockResolvedValueOnce(value)方法为每个测试用例模拟一次已解析的值。
例如。
shopifyService.ts
import Shopify from 'shopify-api-node';
const SHOPIFY_API_VERSION = '';
const bulkOperationStatusQuery = ``;
interface ICurrentBulkOperation {}
export default class ShopifyService {
private readonly shopify: Shopify;
constructor(config: Shopify.IPublicShopifyConfig | Shopify.IPrivateShopifyConfig) {
this.shopify = new Shopify({ ...config, apiVersion: SHOPIFY_API_VERSION });
}
public async getCurrentBulkOperation(): Promise<ICurrentBulkOperation | undefined> {
const response = await this.shopify.graphql(bulkOperationStatusQuery);
return response?.currentBulkOperation;
}
}polly.ts
import ShopifyService from './shopifyService';
export const pollyWolly = {
async cons(accessToken, shopName) {
const shopify = new ShopifyService({ accessToken, shopName });
const m = await shopify.getCurrentBulkOperation();
return m;
},
};polly.test.ts
import { pollyWolly } from './polly';
import ShopifyService from './shopifyService';
const shopifyService = {
getCurrentBulkOperation: jest.fn(),
};
jest.mock('./shopifyService', () => {
return jest.fn(() => shopifyService);
});
describe('66790278', () => {
const accessToken = '123';
const shopName = 'teresa teng';
afterEach(() => {
jest.clearAllMocks();
});
afterAll(() => {
jest.resetAllMocks();
});
it('should return value', async () => {
shopifyService.getCurrentBulkOperation.mockResolvedValueOnce('fake value');
const actual = await pollyWolly.cons(accessToken, shopName);
expect(actual).toEqual('fake value');
expect(shopifyService.getCurrentBulkOperation).toBeCalledTimes(1);
expect(ShopifyService).toBeCalledWith({ accessToken, shopName });
});
it('should return another value', async () => {
shopifyService.getCurrentBulkOperation.mockResolvedValueOnce('another fake value');
const actual = await pollyWolly.cons(accessToken, shopName);
expect(actual).toEqual('another fake value');
expect(shopifyService.getCurrentBulkOperation).toBeCalledTimes(1);
expect(ShopifyService).toBeCalledWith({ accessToken, shopName });
});
});测试结果:
PASS examples/66790278/polly.test.ts (6.536 s)
66790278
✓ should return value (5 ms)
✓ should return another value
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 8.42 shttps://stackoverflow.com/questions/66790278
复制相似问题