Javascript 使用Sinon.JS测试功能链
如何使用Sinon.JS测试如下函数Javascript 使用Sinon.JS测试功能链,javascript,unit-testing,sinon,stub,sinon-chai,Javascript,Unit Testing,Sinon,Stub,Sinon Chai,如何使用Sinon.JS测试如下函数 export function getToken(done) { const kc = Keycloak(config) kc.init({ onLoad: 'login-required' }) .success(authenticated => { authenticated ? done(null, kc.token) : done(new Error('Some error!'), null) })
export function getToken(done) {
const kc = Keycloak(config)
kc.init({ onLoad: 'login-required' })
.success(authenticated => {
authenticated ? done(null, kc.token) : done(new Error('Some error!'), null)
})
.error(() => {
done(new Error('Some error'), null)
})
}
我试着做了如下的事情,但没有成功:
it('should return access_token', () => {
const mockKeycloak = sinon.stub(Keycloak, 'init').returns({
success: () => (true)
})
getToken(function () {})
expect(mockKeycloak.callCount).to.equal(1)
})
基本上,keydepeat from是一种生命,但即使在尝试将
keydepeat
对象存根在窗口引用上,我也无法使其工作。对于在此登陆的任何人,我就是这么做的:
由于keydove是一种生命,一旦我们这样做,它就会覆盖存根对象
const kc = Keycloak(config)
因此,我刚刚从源代码导出了这个对象kc
,并在其上插入了init
方法,它工作得很好