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



//user.dal

我在user.dal中有这两种方法,我正在尝试测试方法1,但是它在function1中有一个请求1(我想伪造这个结果),我正在使用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
    });
  });

来自 import doc:

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

所以当您这样做时:

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

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


然后执行此操作:

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

function1绑定被返回'this is my result'stub取代。

换句话说, function1的模块导出已被Stub

替换

所以当这条线运行时:

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模块会自动支持循环依赖项",因此我们可以将模块返回自身以访问模块导出:

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;
}

如果您遵循此模式并调用模块导出 for function1function2method1内部调用CC_20打电话给method1

时会被打电话给

我认为您应该这样做:method1(id, date, function1, function2)。从本质上讲,您将作为参数传递在函数1中。然后,在测试中,您可以通过模拟功能或存根来进行测试。

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;
}

最新更新