无法存根jwt.verify()

时间:2019-06-04 13:42:55

标签: node.js jwt sinon

我试图为我的API编写一些测试,所以我需要对jwt.veryfy方法进行存根。  但是那里还有一些其他逻辑,所以它不起作用。它返回
TypeError:无法读取null的属性“ args”

 jwt.verify(refreshToken, configData.jwt.refreshKey, (err, decoded) => {
                if (err) {
                    return res.json({
                        success: false,
                        message: 'Token is not valid'
                    });
                } else {
                    let deviceId = decoded.deviceId;
                    let email = decoded.email;
                    console.log('device_id:', deviceId);
                    console.log(email);
                    let token = jwt.sign({email: email, deviceId: deviceId},
                        configData.jwt.secretKey,
                        { expiresIn: '24h' }
                    );
                    console.log(token);
                    res
                        .status(HttpStatus.OK)
                        .send({accessToken: token, refreshToken: refreshToken, tokenExpiration: moment().add(24, 'hours').format('YYYY-MM-DD hh:mm:ss')});
                }
            });
        } else {
            res
                .status(HttpStatus.EXPECTATION_FAILED)
                .send('Auth token is not supplied');
        }

这是我尝试处理的测试代码

describe('LoginController(refreshLoginToken)', function() {
let body = {
    refreshToken: ' '
}

let req = {
    body
}
let options = {
    status: sinon.spy()
}
let res = mockResponse(options)



before(function() {
   sinon.stub(jwt, 'verify')

})

after(function() {
    sinon.restore()
})

it('should refresh login token', async function() {
    await controller.refreshLoginToken(req, res)
    expect(res.status.firstCall.args[0]).to.equal(HttpStatus.OK)
})
})

0 个答案:

没有答案