Question

Je viens de commencer à utiliser Jasmine alors s'il vous plaît pardonnez la question de débutant, mais est-il possible de tester pour les types d'objets lors de l'utilisation toHaveBeenCalledWith?

expect(object.method).toHaveBeenCalledWith(instanceof String);

Je sais que je pourrais cela, mais il est de vérifier la valeur de retour plutôt que l'argument.

expect(k instanceof namespace.Klass).toBeTruthy();
Était-ce utile?

La solution

toHaveBeenCalledWith est une méthode d'un espion. Donc, vous ne pouvez les appeler espion comme décrit dans le docs :

// your class to test
var Klass = function () {
};

Klass.prototype.method = function (arg) {
  return arg;
};


//the test
describe("spy behavior", function() {

  it('should spy on an instance method of a Klass', function() {
    // create a new instance
    var obj = new Klass();
    //spy on the method
    spyOn(obj, 'method');
    //call the method with some arguments
    obj.method('foo argument');
    //test the method was called with the arguments
    expect(obj.method).toHaveBeenCalledWith('foo argument');   
    //test that the instance of the last called argument is string 
    expect(obj.method.calls.mostRecent().args[0] instanceof String).toBeTruthy();
  });

});

Autres conseils

Je l'ai découvert un mécanisme encore plus cool, en utilisant

scroll top