使用sinon和proxyquire的存根相关函数不会打印存根结果

时间:2019-02-25 01:56:54

标签: javascript node.js unit-testing mocha sinon-chai

我为_getuser()编写了单元测试,但没有看到console.log打印存根结果。测试覆盖率还显示了'let user = result.user; console.log('User'+ JSON.stringify(result));'为什么未显示存根结果在LogInCommand类的getUser()函数中的控制台日志中不打印。 我看到结果显示单元测试中未定义。

// userApi.js

    'use strict';
    const config = require('../../config/config');
    const api = require('./apiService');

    class UserApi {
        constructor() {
        }
        getUser(userId) {
            return api.get({
                url: config.url,
                qs: {
                    includeInactive: true,
                    id: userId,
                    startIndex: 0,
                    maxResults: 1
                },
                headers: {
                    Accept: 'application/json;',
                    'Connection': 'Keep-Alive'
                }
            });
        }

    }
    module.exports = UserApi;

    // LoginCommand.js

    'use restrict';

    const userApi = require('../../api/userApi');
    class LogInCommand {
        constructor() {
        }
        async _getUser(userId) {
            let result = await new userApi().getUser(userId);
            let user = result.user;
            console.log('User'+JSON.stringify(result));
            return user;
        }

    }

    module.exports = LogInCommand;

    //LoginCommand.test.js
    describe('Test LogInCommand Class',function(){
      it.only('_getUser function should return user',    async ()=> { 
            let apiData= {
                user:'abc'
            };
          let logincmd = proxyquire('../LogInCommand.js', {
                userApi : { getUser : Promise.resolve(apiData) },'@noCallThru':true});
            let obj= new logincmd();
            let result= await obj._getUser(client);
        });

    });

2 个答案:

答案 0 :(得分:0)

您在当前设置中的proxyquire配置不正确。 Proxyquire将在require调用中传递的字符串值映射到所需的模拟/存根值。请尝试以下操作:

struct{}{}

答案 1 :(得分:0)

以下代码对我有用 // userApi.js

'use strict';
const config = require('../../config/config');
const api = require('./apiService');

class UserApi {
    constructor() {
    }
    getUser(userId) {
        return api.get({
            url: config.url,
            qs: {
                includeInactive: true,
                id: userId,
                startIndex: 0,
                maxResults: 1
            },
            headers: {
                Accept: 'application/json;',
                'Connection': 'Keep-Alive'
            }
        });
    }

}
module.exports = UserApi;

// LoginCommand.js

'use restrict';

const userApi = require('../../api/userApi');
class LogInCommand {
    constructor() {
    }
    async _getUser(userId) {
        let result = await new userApi().getUser(userId);
        let user = result.user;
        console.log('User'+JSON.stringify(result));
        return user;
    }

}

module.exports = LogInCommand;

//LoginCommand.test.js
describe('Test LogInCommand Class',function(){
  it.only('_getUser function should return user',    async ()=> { 
        class userApiStub{
        constructor(){}
        getUser() {
            return  Promise.resolve({
                user:4
            });
        }
    }
      let logincmd = proxyquire('../LogInCommand.js', {
            '../../api/userApi' :userApiStub },'@noCallThru':true});
        let obj= new logincmd();
        let result= await obj._getUser(client);
    });

});