尝试测试方法时,如何模拟函数的请求

时间:2019-03-10 22:26:05

标签: sinon

// user.dal

我在user.dal中有这两种方法,我正在尝试测试method1,但是它在里面有一个叫做function1的请求(我想伪造此结果),我正在使用sinon.stub

export async function function1(id) {
      try {
        const result1 = await User.findOne({ _id: id });
        return result1;
      } catch (error) {
        throw new Error('invalid user');
      }
    }

export async function method1(id, date) {
  const request1 = await function1(id); // this is not faking its results
  const request2 = await function2(request1); // this need to fake the results also
  return request2;
}

// user.test

describe.only('get all information ', () => {
    const id = '5c842bd3cf058d36711c6a9e';
    const user = {
      _id: '5c76f49e6df2131fe23a100a',
    };
    const date = '2019-03-09';
    let spyFunction1;
    beforeEach(async () => {
      spyFunction1 = sinon.stub(userDal, 'function1').returns('this is my result');
    });

    afterEach(async () => {
      await userModel.deleteOne({ _id: id });
      spyFunction1.restore();
    });

    it('Should get.', async () => {
      const result = await userDal.function1(id);
      console.log('this is working well', result);

      const badResult = await userDal.method1(id, date);
      console.log('-->>>', badResult); // when its call to method 1, its calling to the method and not using the mock that I impemented before
    });
  });

1 个答案:

答案 0 :(得分:0)

来自import doc

  

静态import语句用于导入由另一个模块导出的绑定。

因此,当您这样做时:

import * as userDal from './user.dal';

结果是userDal包含对user.dal模块导出的所有内容的绑定。


然后,当您执行此操作时:

sinon.stub(userDal, 'function1').returns('this is my result');

function1绑定被返回stub的{​​{1}}取代。

换句话说,'this is my result'模块导出已由存根替换


因此,当此行运行时:

function1

它正在为const result = await userDal.function1(id); (已存根)调用模块导出,因此结果为function1


另一方面,当此行运行时:

'this is my result'

它输入const badResult = await userDal.method1(id, date); ,然后运行此行:

method1

不会为const request1 = await function1(id); // this is not faking its results 调用模块导出,而它是直接调用function1


为了能够在function1中存根function1function2,您必须调用其模块导出,而不是直接调用它们。


对于method1模块,模式如下:

Node.js

对于ES6模块,模式相似。请注意"ES6 modules support cyclic dependencies automatically",因此我们可以const function1 = async function (id) { /* ... */ } const function2 = async function (id) { /* ... */ } const method1 = async function (id, date) { const request1 = await exports.function1(id); // call the module export const request2 = await exports.function2(request1); // call the module export return request2; } exports.function1 = function1; exports.function2 = function2; exports.method1 = method1; 将一个模块返回自身以获取对模块导出的访问权限:

import

如果您遵循这种模式,并从import * as userDal from 'user.dal'; // import module into itself export async function function1(id) { /* ... */ } export async function function2(id) { /* ... */ } export async function method1(id, date) { const request1 = await userDal.function1(id); // call the module export const request2 = await userDal.function2(request1); // call the module export return request2; } 内为function1function2调用模块导出,那么当您替换模块导出< / em>对于具有存根的这两个函数,当您调用method1时,存根将被调用。