2014-06-20 10 views
25

come nel mio codice.Qual è la differenza tra createspy e createspyobj

return $provide.decorator('aservice', function($delegate) { 
      $delegate.addFn = jasmine.createSpy().andReturn(true); 
      return $delegate; 
     }); 

In che cosa creaSpy? posso cambiare le chiamate createSpy alle chiamate di createspyobj.

Utilizzando createSpy, è possibile creare un mock di funzioni/metodi. Createspyobj può fare più funzioni di simulazione. Ho ragione?

Quale sarebbe la differenza.

risposta

50

jasmine.createSpy può essere utilizzato quando non c'è alcuna funzione da spiare. Troverà le chiamate e gli argomenti come un spyOn ma non c'è implementazione.

jasmine.createSpyObj viene utilizzato per creare una simulazione che spierà su uno o più metodi. Restituisce un oggetto che ha una proprietà per ogni stringa che è una spia.

Se si desidera creare una simulazione, è necessario utilizzare jasmine.createSpyObj. Guarda gli esempi qui sotto.

Dalla documentazione Jasmine http://jasmine.github.io/2.0/introduction.html ...

createSpy:

describe("A spy, when created manually", function() { 
    var whatAmI; 

    beforeEach(function() { 
    whatAmI = jasmine.createSpy('whatAmI'); 

    whatAmI("I", "am", "a", "spy"); 
    }); 

    it("is named, which helps in error reporting", function() { 
    expect(whatAmI.and.identity()).toEqual('whatAmI'); 
    }); 

    it("tracks that the spy was called", function() { 
    expect(whatAmI).toHaveBeenCalled(); 
    }); 

    it("tracks its number of calls", function() { 
    expect(whatAmI.calls.count()).toEqual(1); 
    }); 

    it("tracks all the arguments of its calls", function() { 
    expect(whatAmI).toHaveBeenCalledWith("I", "am", "a", "spy"); 
    }); 

    it("allows access to the most recent call", function() { 
    expect(whatAmI.calls.mostRecent().args[0]).toEqual("I"); 
    }); 
}); 

createSpyObj:

describe("Multiple spies, when created manually", function() { 
    var tape; 

    beforeEach(function() { 
    tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']); 

    tape.play(); 
    tape.pause(); 
    tape.rewind(0); 
    }); 

    it("creates spies for each requested function", function() { 
    expect(tape.play).toBeDefined(); 
    expect(tape.pause).toBeDefined(); 
    expect(tape.stop).toBeDefined(); 
    expect(tape.rewind).toBeDefined(); 
    }); 

    it("tracks that the spies were called", function() { 
    expect(tape.play).toHaveBeenCalled(); 
    expect(tape.pause).toHaveBeenCalled(); 
    expect(tape.rewind).toHaveBeenCalled(); 
    expect(tape.stop).not.toHaveBeenCalled(); 
    }); 

    it("tracks all the arguments of its calls", function() { 
    expect(tape.rewind).toHaveBeenCalledWith(0); 
    }); 
}); 
+0

risposta molto buona, ma cosa succede se voglio il metodo 'gioco' restituire qualcosa? Uso di createSpyObj Non riesco a deridere il comportamento dei metodi – iberbeu

+7

Puoi usare tape.play.and.returnValue (1); vedi [link] (http://jasmine.github.io/2.0/introduction.html#section-Spies:_ and.returnValue) per maggiori dettagli. –

Problemi correlati