How to unit test a filter in AngularJS 1.x

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

How do you unit test a filter in Angular?

5条回答
  •  囚心锁ツ
    2020-12-25 11:12

    Filter can be injected right into test (have found a snippet here)

      describe('myApp', function () {
    
        beforeEach(function () {
          module('myApp');
        });
    
        it('has a bool filter', inject(function($filter) {
          expect($filter('bool')).not.toBeNull();
        }));
    
        it("should return true empty array ", inject(function (boolFilter) {
          expect(boolFilter(true)).toBeTruthy();
        }));
    
      });
    

    In this example filter name is 'bool', in order to inject this filter you shall use 'bool' + 'Filter' = 'boolFilter'

提交回复
热议问题