typescriptでテストしたいメソッドで使用されているメソッドをスタブするのに問題があります。この例では、わかりやすくするために多くのメソッド自体を取り除いていますが、基本的には、getService
メソッドを呼び出すgetServiceWithRetry
メソッドがあります。Sinon - 私がテストしたいメソッドで呼び出されたメソッドをスタブする方法
ie。
export function getServiceWithRetry(name:string, triesLeft:number) {
//do stuff
getService(name)
//do more stuff
}
export function getService(name:string) {
//lookup stuff
}
これはLookup
としてテストにインポートされます。テストでgetService
を呼び出すのであれば、私は正常にgetServiceメソッドをスタブすることができますが、getServiceWithRetry
を実行すると実際のgetService
メソッドとスタブが呼び出されません。誰かが私が間違っていることを知っていますか?
it("test", function(done) {
let serviceStub = sinon.stub(Lookup, 'getService')
serviceStub.returns(Promise.resolve("resolved"))
//this uses the stub
Lookup.getService("name").then(function(value) {
console.log("success: "+value)
}, function(error) {
console.log("error: "+error)
})
//this calls the actual method, not the stub as I would expect it to
Lookup.getServiceWithRetry("serviceName", 4).then(function(value) {
console.log("success: "+value)
}, function(error) {
console.log("error: "+error)
})
done()
})
注:ブルーバードの約束に慣れていない人のために.then(function(value){}, function(error){})
方法は、約束が成功した場合との約束が拒否された場合に何が起こるかを処理します。