首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模拟进行外部API调用的Typescript类

模拟进行外部API调用的Typescript类
EN

Stack Overflow用户
提问于 2021-03-25 06:24:16
回答 1查看 586关注 0票数 1

所以我做了大量的阅读和搜索,但似乎无法弄清楚这一点。

我有一个typescript类,如下所示

代码语言:javascript
复制
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的文件

代码语言:javascript
复制
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的帖子,很多文章,但似乎不太理解。

感谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2021-03-25 10:03:50

我们可以使用Mock using module factory parameter模拟es6类,并使用mockFn.mockResolvedValueOnce(value)方法为每个测试用例模拟一次已解析的值。

例如。

shopifyService.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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 });
  });
});

测试结果:

代码语言:javascript
复制
 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 s
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66790278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档