我有一个基本方法,它接受cookie并返回有效用户
const getInitialState = (id_token) => {
let initialState;
return new Promise((resolve,reject) => {
if(id_token == null){
initialState = {userDetails:{username: 'Anonymous',isAuthenticated: false}}
resolve(initialState)
}else{
var decoded = jwt.verify(JSON.parse(id_token),'rush2112')
db.one('SELECT * FROM account WHERE account_id = $1',decoded.account_id)
.then(function(result){
initialState = {userDetails:{username:result.username,isAuthenticated:true}}
resolve(initialState)
})
.catch(function(err){
console.log('There was something wrong with the token')
reject('There was an error parsing the token')
})
}
})
}
既然这是个承诺-我就用我的承诺。
我可以成功地测试第一个条件(cookie是没有预设)与以下
it('should return anonymous user if id_token isnt preset',function(){
let id_token = null
return expect(getInitialState(id_token)).to.eventually.have.deep.property('userDetails.username','Anonymous')
})
然而,我不能/不太确定如何测试第二部分,因为它同时依赖于jwt和外部db调用(它本身是另一个promise对象)
到目前为止,我已经尝试过了。stub db调用返回这个错误,我发现它非常模糊
1) GetInitialState should return a valid user if id_token is valid:
TypeError: Cannot redefine property: connect
at Function.defineProperty (native)
at Object.wrapMethod (node_modules/sinon/lib/sinon/util/core.js:128:24)
at stub (node_modules/sinon/lib/sinon/stub.js:67:26)
at node_modules/sinon/lib/sinon/stub.js:60:25
at node_modules/sinon/lib/sinon/walk.js:28:30
at Array.forEach (native)
at walkInternal (node_modules/sinon/lib/sinon/walk.js:23:45)
at Object.walk (node_modules/sinon/lib/sinon/walk.js:49:20)
at Object.stub (node_modules/sinon/lib/sinon/stub.js:52:23)
at Context.<anonymous> (getInitialState.spec.js:27:11)
我猜它不允许我stub整个db对象。我还能怎么做呢?我只是希望jwt和db调用都不抛出任何错误。我只是测试如果cookie是有效的,是否会返回一个正确的对象
it('should return a valid user if id_token is valid',function(){
sinon.stub(jwt,'verify').returns({username:foo});
sinon.stub(db).resolves() // unsure of what to do here
id_token = ' Foo----bar '
return expect(getInitialState(id_token)).to.eventually.be.true
})
2条答案
按热度按时间rqcrx0a61#
您需要覆盖一些pg-promises默认值来覆盖特定的方法
特别设置noLocking = true的方法
来自pg-promise作者。这是正确的方法。请参阅选项noLocking:
如果这个规定妨碍了您在测试中使用模型框架,您可以通过在选项中设置noLocking = true来强制库去激活大多数锁。
pg-promise
v11中的接口锁定已全部删除。选项noLocking
不再存在。mspsb9vt2#
应该是