Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 896
user3700866 Avatar asked May 05 '15 19:05

user3700866


1 Answers

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 );
    });
});
like image 199
kevinawalker Avatar answered Sep 26 '22 23:09

kevinawalker