如何在 AngularJS 1.x 中对过滤器进行单元测试 [英] How to unit test a filter in AngularJS 1.x

查看:34
本文介绍了如何在 AngularJS 1.x 中对过滤器进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Angular 中对过滤器进行单元测试?

How do you unit test a filter in Angular?

推荐答案

注入 $filter 然后用 $filter('filterName')(input, options); 调用代码>

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

所以要测试这个模板的等价物 {{ foo |testFilter:capitalize }}

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');
  });
});

这篇关于如何在 AngularJS 1.x 中对过滤器进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆