将依赖注入过滤器以进行单元测试

时间:2015-04-17 13:56:24

标签: angularjs dependency-injection jasmine angularjs-filter

我正在将过滤器注入我的测试,但是我收到一个错误,因为过滤器依赖于下划线。如何将下划线包装器注入过滤器,然后将其注入

错误消息:

Error: [$injector:unpr] Unknown provider: _Provider <- _ <- requestedDataFilter    
茉莉花测试:

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

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

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

过滤:

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

1 个答案:

答案 0 :(得分:2)

你的下划线封装器是你的“MyApp”模块的一部分吗?如果没有,请确保将包含包装器的模块加载到测试中。

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

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

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