2014-06-20 23 views
25

Tôi đã sử dụng mã của tôi như thế nào.Sự khác nhau giữa createdpy và creationpyobj

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

Trong đó createSpy làm gì? tôi có thể thay đổi các cuộc gọi createSpy thành các cuộc gọi createpyobj không.

Bằng cách sử dụng createSpy, chúng tôi có thể tạo một hàm/phương thức mocks. Createspyobj có thể thực hiện nhiều chức năng mocks. Tôi có đúng không?

Điều gì sẽ là sự khác biệt.

Trả lời

50

jasmine.createSpy có thể được sử dụng khi không có chức năng gián điệp. Nó sẽ theo dõi các cuộc gọi và đối số như một spyOn nhưng không có triển khai thực hiện.

jasmine.createSpyObj được sử dụng để tạo mô hình sẽ gián điệp trên một hoặc nhiều phương pháp. Nó trả về một đối tượng có thuộc tính cho mỗi chuỗi là gián điệp.

Nếu bạn muốn tạo mô hình, bạn nên sử dụng jasmine.createSpyObj. Xem các ví dụ bên dưới.

Từ các tài liệu 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

câu trả lời rất tốt nhưng những gì nếu tôi muốn phương pháp 'play' trở về cái gì? Sử dụng createSpyObj Tôi không thể giả lập hành vi của các phương thức – iberbeu

+7

Bạn có thể sử dụng tape.play.and.returnValue (1); xem [link] (http://jasmine.github.io/2.0/introduction.html#section-Spies:_ and.returnValue) để biết thêm chi tiết. –

Các vấn đề liên quan