【发布时间】:2021-06-21 17:00:26
【问题描述】:
所以我做了很多阅读/搜索,但似乎无法弄清楚这一点。
我有一个像这样的打字稿类
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 时,测试可能是 hey,然后确保将消息发送到某个队列等。
我想模拟 shopify.getBulkOperation --> 我不想对 shopify 进行实际的 API 调用...而且我想模拟不同的值。例如,在一个测试用例中,我想想象 getBulkOp 返回 null。在另一个我想想象 getBulkOperation 返回一个具有特定字段等的对象。
我已经阅读了很多 stackoverflow 帖子,很多文章,但似乎无法完全理解。
帮助表示赞赏。
【问题讨论】:
-
你想测试
pollyWolly单元,那么你模拟shopify.getCurrentBulkOperation单元会更好。如果您想测试getCurrentBulkOperation,请模拟shopify.graphql。
标签: typescript unit-testing jestjs mocking ts-jest