请参阅中的代码
我想知道如何添加另一个间谍来窥探$filter(‘date’)返回的方法,以便我可以验证
expect(something,something).toHaveBeenCalledWith('1234','dd-MMM-yyyy');
你应该能够模拟传递给控制器的过滤器,并从这个模拟中返回一个间谍.然后,您可以测试间谍是否正常调用.
例:
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'); }); });