2012-05-16 16 views
17

He creado un ejemplo completo simplificado que replica el problema que estoy recibiendo.no se puede obtener jasmine.any (Función) para trabajar

function TestObj() { 
    var self = this; 
    self.getStuff = function(aString, callback) { 
     // TODO 
    } 
} 

describe("server communications", function() { 
    it("it calls the server", function() { 
     var obj = new TestObj(); 
     obj.getStuff = jasmine.createSpy(); 
     // swap the above line for this and it makes no difference 
     // spyOn(obj, "getStuff"); 

     var functionVar = function() { 
     }; 

     obj.getStuff("hello", functionVar); 

     expect(obj.getStuff).toHaveBeenCalledWith(
       [ "hello", jasmine.any(Function) ]); 
    }); 
}); 

En lugar de una prueba de unidad que pasa, me sale el siguiente resultado:

Esperada espía que han sido llamados con: [[ 'hola', < jasmine.any (Función de función() {[código nativo]})>]] pero fue llamado con: [[ 'hola', function]]

¿por qué no reconocer que las funciones que pasan en (function() {}) en realidad son func ciones? ¿Cuál es el código nativo que está esperando? ¿Alguien más tuvo este problema con jazmín.any (Función)? ¡Gracias!

EDITADO

He intentado utilizar SpyOn en lugar de jasmine.createSpy() y no hace ninguna diferencia. Intenté solo un argumento y funciona. La introducción del primer argumento de cadena rompe el jazmín.any (Función) - ¿Alguna idea?

Respuesta

31

Ah, pensé que tenía que adjuntar el argumento s de expect().toHaveBeenCalledWith en un Array[]. Tonto de mí. Aquí está una versión de trabajo:

function TestObj() { 
    var self = this; 
    self.getStuff = function(aString, callback) { 
     // TODO 
    } 
} 

describe("server communications", function() { 
    it("it calls the server", function() { 
     var obj = new TestObj(); 
     obj.getStuff = jasmine.createSpy(); 
     // swap the above line for this and it makes no difference 
     // spyOn(obj, "getStuff"); 

     var functionVar = function() { 
     }; 

     obj.getStuff("hello", functionVar); 

     expect(obj.getStuff).toHaveBeenCalledWith("hello", jasmine.any(Function)); 
    }); 
}); 
7

El problema es la manera de crear su espía, utilizando spyOn parece funcionar como se esperaba:

describe("test", function() { 
    return it("is function", function() { 
    var a = {b: function(){}}; 
    spyOn(a, 'b'); 
    a.b(function(){}); 
    expect(a.b).toHaveBeenCalledWith(jasmine.any(Function)); 
    }); 
}); 

También write your own Matcher podría poner a prueba si el valor pasado es una función:

describe('test',function(){ 
    it('is function',function(){ 

    this.addMatchers({ 
     isFunction: function() { 
     return typeof this.actual === 'function'; 
     } 
    }); 
    expect(function(){}).isFunction(); 
    }); 
}); 

EDITADO: codificó el primer fragmento de código

+0

Ver mis ediciones a mi respuesta original. Probé spyOn - no hay diferencia. ¿Has conseguido un ejemplo para trabajar con múltiples argumentos? –

+0

nevermind - lo arregló. Gracias por tu respuesta. –

Cuestiones relacionadas