2016-10-28 2 views
1

Wie kann ich Function.prototype.apply Methode auf diesen Code testen?Testen von JavaScript anwenden Methode mit Sinon

var yoGen = require('yeoman-generator'); 

exports.method = function() { 

    yoBen.Base.apply(this, arguments); 

}; 

Wie kann ich behaupten, dass apply mit den richtigen Argumenten auf dem richtigen Modul aufgerufen wurde? Ich versuche, wie dies aber nicht:

var apply = sinon.stub(yoGen.Base,'apply'); 
testingModule.method(); 
sinon.assert.calledWith(apply,testingModule,{}); 

Der Fehler Assertion ist:

apply({ ... content of testingModule ... }) != { ... content of testingModule ... } 
+0

Was soll das Modul tun? Ist es eine Funktion, die etwas zurückgibt? – staypuftman

+0

@staypuftman Ich habe Frage aktualisiert ... btw seine Funktion ... – urosjarc

Antwort

0

Function.prototype.apply ist nur eine kurze Hand ein Objekts in einer Schleife durch eine Funktion. MDN zeigt ein pretty good example to follow:

var numbers = [5, 6, 2, 3, 7]; 

// using Math.min/Math.max apply 
var max = Math.max.apply(null, numbers); 
// This about equal to Math.max(numbers[0], ...) 
// or Math.max(5, 6, ...) 

In Ihrem Beispiel, ist es nicht 100% klar ist, was Sie yoBen Funktion tut, aber Sie würden durch Ihre arguments werden Looping und konnte das Ergebnis wie folgt nur Ausgabe:

// Put apply into a variable  
var results = yoBen.Base.apply(this, arguments); 

// Just output the results and see what happened 
console.log(results); 
+0

Die 'JSON.stringify (testingModule)' nach dem Ausführen der Methode ist 'apply ({... Inhalt von testingModule ...}) ', wie die Hölle kann ich das testen? – urosjarc

+0

Einen Schritt zurückgehen - was 'console.log (anwenden);' ausgeben. – staypuftman

+0

Die Anwendung ist native 'Function.prototype.apply'. Also gilt: "Apply" ist nicht definiert. – urosjarc

Verwandte Themen