user3700866
user3700866

Reputation: 151

How to provide mock filters for unit testing

I have a Controller that has this line of code

 var formattedDate= $filter('date')(dateColName,short);

I am trying unit test this controller and I am not clear on how to mock the date filter in my code.

Upvotes: 11

Views: 8910

Answers (1)

kevinawalker
kevinawalker

Reputation: 549

You need to add 'Filter' to the end of your filter name when mocking a filter in Angular, as Angular stores filter like services, but adds 'Filter' to the end. Try this for example:

var mockFilter = function() {
    return 'whatyouwantittoreturn';
};

beforeEach(function() {
    module(function($provide) {
        $provide.value('dateFilter', mockFilter );
    });
});

Upvotes: 21

Related Questions