在使用Jasmine的NodeJ的单元测试中,我试图伪造或模拟axios,使其看起来登录失败(当我们不会获得响应时返回access_token)或成功(当我们得到响应时,返回响应时) 。
我正在做的就是在函数中添加第三个可选参数,就像这样
callAuthServer( username, password, axiosI = axios ) {
然后为我的测试构建了自己的假axios对象以进行验证,是否可以正确调用它?
下面的代码是我编写的单元测试:
describe('Calling Auth Server', function() {
it('Validate username:password', function() {
let didCall = false
let fakeAxios = {
defaults: {
headers: {
common: {},
post: {},
},
},
post: (url, data) => {
return new Promise((resolve, reject) => {
didCall = true
resolve( { response: { data: { access_token: 42 } } } )
})
},
}
authService.callAuthService('abbbcuuc', 'ssbsbsb', fakeAxios)
.then( token => {
expect(token).toBeDefined()
expect(didCall).toBe(true)
} )
.catch( err => {
expect(err).not.toBeDefined(err)
} )
})
})
但是我遇到错误了,我不确定为什么错误说access_toke是未定义的,因为它已经定义了
失败:
1)调用Auth Server验证用户名:密码
消息:
Expected TypeError: Cannot read property 'access_token' of undefined not to be defined TypeError: Cannot read property 'access_token' of undefined.
堆栈:
Error: Expected TypeError: Cannot read property 'access_token' of undefined not to be defined TypeError: Cannot read property 'access_token' of undefined.
任何帮助和建议将不胜感激:-)