1

In controller I have imported config file for getting path for userId and secret path location and auth file for getting the userId name and secret value based on that I have redirected to some endpoint. but when I write the testcases how to pass or get the all details using stub. can you please assist.

controller.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');
 });
 });

1 Answers1

0

It's not clear what you are trying to achieve. If you want to create stubs that can act differently in response to different arguments. You can use stub.withArgs(arg1[, arg2, ...]).

E.g.

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

Test result:

  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 |                   
--------------------|---------|----------|---------|---------|-------------------
Lin Du
  • 88,126
  • 95
  • 281
  • 483