如何在Angular中单元测试一个过滤器?
注入$过滤器,然后调用它$ filter(‘filterName’)(input,options);
所以要测试相当于这个模板{{foo | testFilter:capitalize}}
describe('The test filter',function () { 'use strict'; var $filter; beforeEach(function () { module('myTestFilterModule'); inject(function (_$filter_) { $filter = _$filter_; }); }); it('should capitalize a string',function () { // Arrange. var foo = 'hello world',result; // Act. result = $filter('testFilter')(foo,'capitalize'); // Assert. expect(result).toEqual('HELLO WORLD'); }); });