2017-02-20 6 views
0

Ich versuche, eine Methode stub, die Argumente nimmt.Stub eine Funktion mit Argumenten

ich mein Objekt normalerweise etwa so:

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

Dies funktioniert. Es gibt entweder null oder ein Objekt zurück.

ich normalerweise diese Stummel etwa so:

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

ich möchte es Stummel so dass die Argumente berücksichtigt werden. Ich habe gelesen, http://sinonjs.org/docs/#stubs und es soll wie die aussehen folgende:

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

ich den Fehler jedoch erhalten:

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); Ergebnisse in:

[Function: find] 
+0

Was ist in Zeile 67, Spalte 26 von stub.js? – rasmeister

Antwort

0

Arghhh, war ich fast richtig. Unten funktioniert Code:

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