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

how can I mock the request of a function when I try to test a method

//user.dal

我在 user.dal 中有这两个方法,我正在尝试测试方法 1,但它有一个名为 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
    });
  });

我认为你应该像这样制作方法签名:method1(id, date, function1, function2)。您实际上是将 function1 作为参数传递。然后在您的测试中,您可以改为传入模拟函数或存根以便能够对其进行测试。

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, function1, function2) {
  const request1 = await function1(id); 
  const request2 = await function2(request1);
  return request2;
}

来自import doc

The static import statement is used to import bindings which are exported by another module.

所以当你这样做时:

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

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


那么当你这样做时:

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

function1 绑定被替换为 stub 其中 returns '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


为了能够在 method1 中存根 function1function2 您必须调用它们的 模块导出 而不是直接调用它们.


对于 Node.js 模块,模式如下所示:

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;

对于 ES6 模块,模式类似。请注意 "ES6 modules support cyclic dependencies automatically" 这样我们就可以 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;
}

如果您遵循此模式并从 method1 中为 function1function2 调用 模块导出 ,那么当您替换 module exports 对于这两个带有存根的函数,存根将在您调用 method1.

时被调用