【问题标题】:inject dependency into filter for unit test将依赖项注入过滤器以进行单元测试
【发布时间】:2015-06-24 10:16:37
【问题描述】:

我将过滤器注入到我的测试中,但由于过滤器依赖于下划线,所以我收到了错误消息。如何在将下划线包装器注入过滤器之前将其注入过滤器

错误信息:

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

【问题讨论】:

    标签: angularjs dependency-injection jasmine angularjs-filter


    【解决方案1】:

    您的下划线包装器是“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();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多