How to unit test a filter in AngularJS 1.x

后端 未结 5 1441
深忆病人
深忆病人 2020-12-25 10:26

How do you unit test a filter in Angular?

5条回答
  •  梦毁少年i
    2020-12-25 11:03

    Inject $filter and then call it with $filter('filterName')(input, options);

    So to test the equivalent of this template {{ 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');
      });
    });
    

提交回复
热议问题