测试扩展类时的基础 ES6 类(超级方法)的 Jest 模拟方法 [英] Jest mock method of base ES6 class (super method) when testing extended class

查看:22
本文介绍了测试扩展类时的基础 ES6 类(超级方法)的 Jest 模拟方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试扩展类时,我在测试使用某些参数调用原始方法(来自基类)时遇到问题.我要测试的课程是:

I am having issues when testing that the original method (from the base class), is called with some parameters, when testing an extended class. The class that I want to test is:

// ApiDataSource.js
import { RESTDataSource } from "apollo-datasource-rest";

export default class ApiDataSource extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = 'test';
  }

  //We add the authorization params to the get method
  async get(path, params = {}, init = {}) {
    return super.get(
      path,
      {
        ...params,
        app_id: "test app id",
        app_key: "test app key",
      },
      init
    );
  }
}

基本上,我想模拟 super.get() 来断言当 ApiDataSource.get() 被调用时,super 使用授权参数调用方法.

And basically, I would want to mock super.get() to assert that when ApiDataSource.get() is called, the super method is called with the authorization parameters.

类似于:

// ApiDataSource.test.js
import ApiDataSource from './ApiDataSource'

// ...
test("adds the authorization parameters to the get call", async () => ({
  const class = new ApiDataSource();
  await class.get("test");

  expect(mockedSuperGet).toHaveBeenCalledWith("test", {app_id: "test app id", app_key: "test app key"})
});

知道怎么做吗?尝试过 jest.mock() jest.spyOn 等等,我似乎无法理解...

Any idea how to do that? have tried jest.mock() jest.spyOn and so on and I can't seem to get it...

CodeSandbox: https://codesandbox.io/s/example-test-api-data-source-6ldpk?file=/src/ApiDataSource.test.ts

推荐答案

您可以使用 jest.spyOn(object, methodName)RESTDataSource.prototype.get 方法创建一个模拟函数.

You can use jest.spyOn(object, methodName) to create a mock function for RESTDataSource.prototype.get method.

例如

ApiDataSource.js:

import { RESTDataSource } from 'apollo-datasource-rest';

export default class ApiDataSource extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = 'test';
  }

  async get(path, params = {}, init = {}) {
    return super.get(
      path,
      {
        ...params,
        app_id: 'test app id',
        app_key: 'test app key',
      },
      init
    );
  }
}

ApiDataSource.test.js:

import { RESTDataSource } from 'apollo-datasource-rest';
import ApiDataSource from './ApiDataSource';

describe('65391369', () => {
  test('adds the authorization parameters to the get call', async () => {
    const mockedSuperGet = jest.spyOn(RESTDataSource.prototype, 'get').mockResolvedValueOnce('fake data');
    const apiDataSource = new ApiDataSource();
    await apiDataSource.get('test');
    expect(mockedSuperGet).toHaveBeenCalledWith('test', { app_id: 'test app id', app_key: 'test app key' }, {});
    mockedSuperGet.mockRestore();
  });
});

单元测试结果:

 PASS  examples/65391369/ApiDataSource.test.js (5.273 s)
  65391369
    ✓ adds the authorization parameters to the get call (4 ms)

------------------|---------|----------|---------|---------|-------------------
File              | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------------|---------|----------|---------|---------|-------------------
All files         |     100 |      100 |     100 |     100 |                   
 ApiDataSource.js |     100 |      100 |     100 |     100 |                   
------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        6.28 s

这篇关于测试扩展类时的基础 ES6 类(超级方法)的 Jest 模拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆