Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
195
rated 0 times [  199] [ 4]  / answers: 1 / hits: 35636  / 11 Years ago, mon, january 13, 2014, 12:00:00

How do you unit test a filter in Angular?


More From » angularjs

 Answers
14

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

[#73206] Sunday, January 12, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
agustindejonm

Total Points: 738
Total Questions: 84
Total Answers: 84

Location: Northern Ireland
Member since Mon, Nov 14, 2022
2 Years ago
agustindejonm questions
Fri, Jun 25, 21, 00:00, 3 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Sat, May 16, 20, 00:00, 4 Years ago
;