2017-02-20 1 views
0

인수를 취하는 메소드를 스텁하려고합니다.인수를 사용하여 함수를 스텁합니다.

나는 보통과 같이 내 객체를 사용 :이 작동

const res = await Obj.find('admin', 'type'); 

합니다. null 또는 객체를 돌려줍니다.

나는 보통과 같이이 스텁 :

sandbox.stub(Obj.prototype, 'find', function() { 
    return Promise.resolve({ id: 123 }); 
}); 

내가 인수가 고려 있도록 스텁 싶습니다. 나는 http://sinonjs.org/docs/#stubs을 읽어 봤는데 다음과 같이하기로했다 :에

TypeError: Attempted to wrap undefined property undefined as function 
    at Object.wrapMethod (node_modules/sinon/lib/sinon/util/core.js:114:29) 
    at Object.stub (node_modules/sinon/lib/sinon/stub.js:67:26) 

console.log(Obj.prototype.find); 결과 : 나는 그러나 오류 얻을

const stub = sinon.stub(Obj.prototype.find); 
stub.withArgs('admin', 'type') 
    .returns(Promise.resolve({ id: 123 })); 
stub.withArgs('user', 'type').returns(null); 

, 내가

[Function: find] 
+0

stub.js의 67 행 26 열은 무엇입니까? – rasmeister

답변

0

Arghhh이었다 거의 정확합니다. 아래 코드는 작동합니다 :

const stub = sinon.stub(Obj.prototype, 'find'); 
stub.withArgs('admin', 'type') 
    .returns(Promise.resolve(new User({ id: 123 }))); 
stub.withArgs('user', 'type').returns(null); 
관련 문제