如何在Angular单元测试

时间:2015-12-18 18:17:21

标签: angularjs unit-testing underscore.js karma-jasmine

我试图在工厂中对一个简单的功能进行单元测试,但我在代码中使用了underscorejs库。

要测试的代码:

        var unpackOrderData = function(data){
        var paramsToUnpack = ["created_at", "total_price"];

        var unpackedArray = _.map(data, function(largeObj){
            return _.pick(largeObj, paramsToUnpack);
        });

        _.each(unpackedArray, convertTimeToMillis);

        _.each(unpackedArray, convertJSONToArray);

        var returnValue = [unpackedArray];
        return returnValue;
    };

app.js:

.constant(' &#39 ;,       窗口。   )

    beforeEach( 
    inject(function(_$rootScope_, _$httpBackend_, _OrderDataFactory_, ___) {
        $rootScope  = _$rootScope_;
        _ = ___;
        httpBackend = _$httpBackend_;
        httpBackend.whenGET('http:/localhost:1000/orders').respond(200, dataToCompare);

        orderDataFactory = _OrderDataFactory_;
    }) 
);

然后我从业力结果中得到以下错误:         TypeError:' undefined'不是一个对象(评估' _。map')

我有什么想法可以将这种依赖注入我的测试中吗?

1 个答案:

答案 0 :(得分:0)

您应该在karma.conf文件中导入undescore库

在我的代码中,我确实是这样的:

files: [
            { pattern: 'node_modules/angular/angular.js', watched: false },
            { pattern: 'node_modules/angular-mocks/angular-mocks.js', watched: false },
            { pattern: 'node_modules/underscore/underscore.js', watched: false },