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

问题描述 投票:0回答:2

//user.大量

我在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
    });
  });
sinon
2个回答
0
投票

来自import doc

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

所以当你这样做时:

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

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


然后当你这样做:

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

function1绑定被stub取代,'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调用模块导出,而是直接调用function1


为了能够在function2中存根method1Node.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 modules support cyclic dependencies automatically"

对于ES6模块,模式类似。请注意,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; } 模块重新置于自身以获取对模块导出的访问权限:

function1

如果您遵循此模式并从function2中调用method1method1的模块导出,那么当您使用存根替换这两个函数的模块导出时,将在调用method1(id, date, function1, function2)时调用存根。


0
投票

我认为你应该像这样制作方法签名: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; } 。你基本上将function1作为参数传递。然后在测试中,您可以传入模拟函数或存根以便能够测试它。

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.