2014-09-04 17 views
8

Xin vui lòng xem mã trong

http://jsfiddle.net/2Ny8x/69/

Tôi tự hỏi làm thế nào tôi có thể thêm gián điệp khác để spyOn phương pháp trả về bởi $ lọc ('ngày ') để tôi có thể xác minh

expect(something, something).toHaveBeenCalledWith('1234', 'dd-MMM-yyyy'); 

Trả lời

14

Bạn có thể giả lập bộ lọc được chuyển vào bộ điều khiển và trả lại một gián điệp từ mô hình này. Sau đó bạn có thể kiểm tra rằng điệp viên được gọi là bình thường.

Ví dụ:

describe('MyCtrl', function() { 
    var filter, innerFilterSpy, http, scope; 

    beforeEach(inject(function ($rootScope, $controller, $http) { 
    http = $http; 
    innerFilterSpy = jasmine.createSpy(); 
    filter = jasmine.createSpy().and.returnValue(innerFilterSpy); 
    scope = $rootScope.$new(); 
    controller = $controller('MyCtrl', { 
     '$scope': scope, 
     '$http': http, 
     '$filter': filter 
    }); 
    })); 

    it('call $filter("date") and test()', function() { 
     expect(scope.date).toBe('01-Jan-1970'); 
     expect(http.get).toHaveBeenCalled(); 
     expect(filter).toHaveBeenCalledWith('date'); 
     expect(innerFilterSpy).toHaveBeenCalledWith('1234', 'dd-MMM-yyyy'); 
    }); 
}); 
+0

này đã giúp nhưng tôi muốn chỉ ra nó gây hiểu lầm để bơm $ lọc kể từ khi bạn không sử dụng nó. – Alex

+0

Cảm ơn, tôi đã xóa nội dung đó ngay bây giờ. – fiznool

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