首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用proxyquire和sinon调用导入函数

如何使用proxyquire和sinon调用导入函数
EN

Stack Overflow用户
提问于 2022-01-11 11:48:17
回答 1查看 656关注 0票数 0

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

controller.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
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',
      )
  });
});

在运行测试用例时获取错误。请协助。

EN

回答 1

Stack Overflow用户

发布于 2022-01-12 02:20:19

从医生那里:

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

要测试的模块的

  • request:路径(例如,表单{ modulePath: stub, ... }模块路径的../lib/foo
  • stubs:键/值对相对于测试模块,而不是测试文件),因此指定它与测试文件中的Require语句完全相同:测试文件值本身是函数/属性的键/值对,适当的重写

例如。

controller.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
export function getSecret(path) {
  return 'real implementation';
}

controller.test.ts

代码语言:javascript
复制
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');
  });
});

测试结果:

代码语言:javascript
复制
  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                 
------------------|---------|----------|---------|---------|-------------------
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70666283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档