2014-06-20 10 views
25

Kodumda kullandığım gibi.Createpy ve createspyobj arasındaki fark nedir?

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

Bu şekilde createSpy ne yapar? createpyobj çağrılarına createSpy çağrılarını değiştirebilir miyim.

CreateSpy kullanarak, tek bir işlev/yöntem alay oluşturabiliriz. Createspyobj çoklu işlevler alay yapabilir. Haklı mıyım

Fark ne olurdu? Gözetlemek için bir işlev olmadığında jasmine.createSpy

cevap

50

jasmine.createSpy kullanılabilir. Bir spyOn gibi aramaları ve argümanları izler, ancak hiçbir uygulama yoktur.

jasmine.createSpyObj, bir veya daha fazla yöntem üzerinde casusluk yapacak bir sahte oluşturmak için kullanılır. Her bir dize için bir casus olan bir özelliği olan bir nesne döndürür.

Bir alay oluşturmak istiyorsanız, jasmine.createSpyObj'u kullanmalısınız. Aşağıdaki örneklere göz atın. Yasemin belgelerine http://jasmine.github.io/2.0/introduction.html itibaren

...

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

Çok iyi bir cevap ama ne bir şey dönmek için yöntem 'play' isterseniz? CreateSpyObj kullanma Ben – iberbeu

+7

yöntemlerinin davranışını alamıyorum tape.play.and.returnValue (1); Daha fazla bilgi için [link] sayfasına bakın (http://jasmine.github.io/2.0/introduction.html#section-Spies:_ and.returnValue). –