如何以多种方式调用或存根相同的导入函数 [英] How to call or stub with same imported function with multiple ways
本文介绍了如何以多种方式调用或存根相同的导入函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Controler.ts
import { userConf } from './conf';
import {userSec} from './auth'
export function userInfo(req: Request, res: Response) {
const path='test:path';
const path1='test:path1'
const userID = userConf(path); //user/Id
const userPass=userConf(path1); // user/pass
const secId=userSec(userID); // raj
const secPass=userSec(userPass) // Otersg
const url=`https://mapuser.com?userId=${secId}&usersec=${secPass}`;
res.redirect(302,url);
}
Details.json
{
"test":{
"path":"user/Id",
"path1":"user/pass"
}
}
conf.ts
export function userConf(path) {
return 'implementation for getting path';
}
Auth.ts
export function usersec(path) {
return 'implementation for getting values';
}
test.spec.ts
import sinon from 'sinon';
import proxyquire from 'proxyquire';
describe('should redirect', () => {
it('should pass with all valid', () => {
const getpath = sinon.stub().returns('test:path');
const getsecId = sinon.stub().returns('ram');
const urlctl = proxyquire('./controller', {
'./conf': {
userConf: getpath,
},
'./auth': {
userSec: getsecId,
},
});
const req = {};
const res = { redirect: sinon.stub() };
urlctl.getId(req, res);
sinon.assert.calledWithExactly(res.redirect, 302, 'https://mapuser.com?
userId=raj&usersec=Otersg');
});
});
推荐答案
不清楚您要尝试实现的目标。如果您想要创建可以根据不同参数做出不同反应的存根。您可以使用stub.withArgs(arg1[, arg2, ...])
。
例如
controller.ts
:
import { Request, Response } from 'express';
import { userConf } from './conf';
import { userSec } from './auth';
export function userInfo(req: Request, res: Response) {
const path = 'test:path';
const path1 = 'test:path1';
const userID = userConf(path);
const userPass = userConf(path1);
const secId = userSec(userID);
const secPass = userSec(userPass);
const url = `https://mapuser.com?userId=${secId}&usersec=${secPass}`;
res.redirect(302, url);
}
controller.spec.ts
:
import sinon from 'sinon';
import proxyquire from 'proxyquire';
describe('should redirect', () => {
it('should pass with all valid', () => {
const getpath = sinon.stub();
getpath.withArgs('test:path').returns('fake user id');
getpath.withArgs('test:path1').returns('fake user pass');
const getsecId = sinon.stub();
getsecId.withArgs('fake user id').returns('raj');
getsecId.withArgs('fake user pass').returns('Otersg');
const urlctl = proxyquire('./controller', {
'./conf': {
userConf: getpath,
},
'./auth': {
userSec: getsecId,
},
});
const req = {};
const res = { redirect: sinon.stub() };
urlctl.userInfo(req, res);
sinon.assert.calledWithExactly(res.redirect, 302, 'https://mapuser.com?userId=raj&usersec=Otersg');
});
});
测试结果:
should redirect
✓ should pass with all valid (239ms)
1 passing (243ms)
--------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------|---------|----------|---------|---------|-------------------
All files | 93.33 | 100 | 60 | 93.33 |
auth.ts | 50 | 100 | 0 | 50 | 2
conf.ts | 50 | 100 | 0 | 50 | 2
controller.spec.ts | 100 | 100 | 100 | 100 |
controller.ts | 100 | 100 | 100 | 100 |
--------------------|---------|----------|---------|---------|-------------------
这篇关于如何以多种方式调用或存根相同的导入函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文