Timigen
Timigen

Reputation: 1025

inject dependency into filter for unit test

I am injecting the filter into my tests but I am getting an error because the filter has a dependency on underscore. How can I inject my underscore wrapper into the filter before injecting it into

Error msg:

Error: [$injector:unpr] Unknown provider: _Provider <- _ <- requestedDataFilter    

jasmine test:

beforeEach(function () {
    module('myApp');

    inject(function (_requestedData_) {
        requestedData = _requestedData_;
    });
});

it("should exist", function () {
    expect(angular.isFunction(requestedData)).toBeTruthy();
});

Filter :

angular.
    module("myApp").
    filter("requestedData", [
    "_",
function (_) {
    "use strict";

    var
        getRequestedData = function (index, filters, dataTable) {
            var
                filter = filters[index],
                requestedData;

            if (filter.items.length > 0) {
                requestedData = _.filter(dataTable, function (row) {
                    return _.contains(filter.items, row[filter.index]);
                });
            } else {
                requestedData = dataTable;
            }

            return (++index < filters.length ? getRequestedData(index, filters, requestedData) : requestedData);
    };

    return getRequestedData;
}]);

Upvotes: 1

Views: 1091

Answers (1)

AndrewJ
AndrewJ

Reputation: 120

Is your underscore wrapper part of your "MyApp" module? If not, make sure you load the module that contains your wrapper into your test.

beforeEach(function () {
    module("underscore");//add underscore wrapper module
    module("myApp");

    inject(function (_requestedData_) {
        requestedData = _requestedData_;
    });
});

it("should exist", function () {
    expect(angular.isFunction(requestedData)).toBeTruthy();
});

Upvotes: 2

Related Questions