【问题标题】:How to call imported function using proxyquire and sinon如何使用 proxyquire 和 sinon 调用导入的函数
【发布时间】:2022-01-11 11:48:17
【问题描述】:

在我的控制器中,当我使用路径作为参数调用该 util 时,我已经导入了安全 util 文件,它返回唯一的 Id。但是如何使用 proxyquire 和 stub 在测试文件中调用这个函数。

controller.ts

import { getSecret } from './util/secrect-util'

export function getId(req: Request, res: Response) {
    const path='test/path'
    const uniueID = getSecret(path);
    console.log(uniueID) // prints testUser01
    const url=`https://mytest.com?userid=${uniueID}`;
    res.redirect(302,url);
}

test.ts

import { Request, Response } from "express";
import * as sinon from 'sinon';
import * as proxyquire from 'proxyquire';

describe('should redirect', () => {
    const validurl:string="https://mytest.com?userid=testUser01";
    let res:any;
    let req:any

    beforeEach(() => {
        res = {
            redirect:sinon.stub()
       }
   });

   it('should get error with invalid path', () => {
       const secPath = sinon.stub().returns('/test/invalidPath');
       const urlctl = proxyquire('./controller', {
           getSecret: { path: secPath },
       });
      urlctl.getId(req, res);
      sinon.assert.calledWithExactly(
          res.redirect,
          400,
          'inValidpath',
      )
  });
});

运行测试用例时出错。请帮忙。

【问题讨论】:

  • descripbe 在 test.js 中应该是 describe
  • 哦,更新了@Ameer,谢谢。

标签: node.js express unit-testing sinon proxyquire


【解决方案1】:

来自文档:

proxyquire({string} request, {Object} stubs)

  • request:要测试的模块的路径,例如../lib/foo
  • 存根{ modulePath: stub, ... } 形式的键/值对 模块路径是相对于测试模块而不是测试文件 因此完全按照测试文件中的 require 语句指定它 值本身是函数/属性的键/值对和适当的覆盖

例如

controller.ts:

import { getSecret } from './util/secrect-util';
import { Request, Response } from 'express';

export function getId(req: Request, res: Response) {
  const path = 'test/path';
  const uniueID = getSecret(path);
  console.log(uniueID);
  const url = `https://mytest.com?userid=${uniueID}`;
  res.redirect(302, url);
}

util/secrect-util.ts:

export function getSecret(path) {
  return 'real implementation';
}

controller.test.ts:

import sinon from 'sinon';
import proxyquire from 'proxyquire';

describe('70666283', () => {
  it('should pass', () => {
    const getSecretStub = sinon.stub().returns('123');
    const urlctl = proxyquire('./controller', {
      './util/secrect-util': {
        getSecret: getSecretStub,
      },
    });
    const req = {};
    const res = { redirect: sinon.stub() };
    urlctl.getId(req, res);
    sinon.assert.calledWithExactly(getSecretStub, 'test/path');
    sinon.assert.calledWithExactly(res.redirect, 302, 'https://mytest.com?userid=123');
  });
});

测试结果:

  70666283
123
    ✓ should pass (1743ms)


  1 passing (2s)

------------------|---------|----------|---------|---------|-------------------
File              | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------------|---------|----------|---------|---------|-------------------
All files         |   88.89 |      100 |      50 |   88.89 |                   
 70666283         |     100 |      100 |     100 |     100 |                   
  controller.ts   |     100 |      100 |     100 |     100 |                   
 70666283/util    |      50 |      100 |       0 |      50 |                   
  secrect-util.ts |      50 |      100 |       0 |      50 | 2                 
------------------|---------|----------|---------|---------|-------------------

【讨论】:

  • 如何处理具有相同功能的多个变量,如下所示export function getId(req: Request, res: Response) { const path = 'test/path'; const path1 = 'test/path1'; const uniueID = getSecret(path); const uniueID1 = getSecret(path1); console.log(uniueID); const url = mytest.com?userid=${uniueID}?userid1=${uniueID1}; res.redirect(302, url); }
猜你喜欢
  • 2020-10-24
  • 2018-08-12
  • 2017-12-20
  • 2015-07-10
  • 2021-02-16
  • 2021-09-13
  • 2017-08-02
  • 2018-10-15
  • 1970-01-01
相关资源
最近更新 更多