单元测试通过Angular依赖注入失败了

时间:2016-06-22 20:09:54

标签: javascript angularjs unit-testing karma-jasmine

我试图创建一个简单的单元测试来访问名为LimboExpenseService的工厂。

angular.module( 'Trackee').factory( 'LimboExpenseService', require( './services/LimboExpenseService'))

我的测试看起来像:

describe('LimboService', function() {
    var Limbo;
    beforeEach(module('Trackee'));


    it('should have a LimboExpenseService ', inject(['LimboExpenseService', function(LimboExpenseService) {
        expect(LimboExpenseService).toBeUndefined();
    }])); 
});

问题是,我似乎无法通过inject()方法获取该项目。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

尝试在inject块内调用beforeEach函数。这应该有用。

以下是:



describe('LimboExpenseService', function() {
    var LimboExpenseService;
    beforeEach(module('Trackee'));
    beforeEach(inject(function(_LimboExpenseService_){
    	LimboExpenseService = _LimboExpenseService_;
    }))

    it('should have a LimboExpenseService ',function(){
    	expect(LimboExpenseService).toBeDefined();
    }); 
});