Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
模拟进行外部API调用的Typescript类_Typescript_Unit Testing_Jestjs_Mocking_Ts Jest - Fatal编程技术网

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

模拟进行外部API调用的Typescript类,typescript,unit-testing,jestjs,mocking,ts-jest,Typescript,Unit Testing,Jestjs,Mocking,Ts Jest,所以我做了很多阅读/搜索,但似乎无法理解这一点 我有一个typescript类,看起来像这样 import * as Shopify from 'shopify-api-node'; export default class ShopifyService { private readonly shopify: Shopify; constructor( config: Shopify.IPublicShopifyConfig | Shopify.IPrivateShopifyC

所以我做了很多阅读/搜索,但似乎无法理解这一点

我有一个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;
  }
}
我正在为pollyWolly写测试。例如,当cons运行并且shopify.getCurrentBulkOperation返回null时,测试可能是hey,然后确保消息被发送到某个队列等

我想模拟shopify.getBulkOperation-->我不想对shopify进行实际的API调用。。。我想用不同的价值观来嘲笑。例如,在一个测试用例中,我想想象getBulkOp返回null。在另一个例子中,我想想象getBulkOperation返回了一个带有特定字段等的对象

我读过很多stackoverflow的帖子,很多文章,但似乎不太明白


感谢帮助。

我们可以使用模拟es6类,并使用方法为每个测试用例模拟一次解析值

例如

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;
  }
}
import ShopifyService from './shopifyService';

export const pollyWolly = {
  async cons(accessToken, shopName) {
    const shopify = new ShopifyService({ accessToken, shopName });
    const m = await shopify.getCurrentBulkOperation();
    return m;
  },
};
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 });
  });
});
polly.test.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;
  }
}
import ShopifyService from './shopifyService';

export const pollyWolly = {
  async cons(accessToken, shopName) {
    const shopify = new ShopifyService({ accessToken, shopName });
    const m = await shopify.getCurrentBulkOperation();
    return m;
  },
};
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 s

您想测试
pollyWolly
unit,那么如果您模拟
shopify.getCurrentBulkOperation
unit会更好。如果您想测试
getCurrentBulkOperation
,让mock
shopify.graphql