在简单的Jasmine测试中注入$ log服务

时间:2014-01-14 16:45:32

标签: angularjs jasmine angular-mock

是的,我是Angular和Jasmine的新手,我无法弄清楚如何为我的测试注入一个模拟$ log。这是测试:

(function () {
    'use strict';

    describe('basic test', function(){
        it('should just work', function(){
            var $log;

            beforeEach(inject(function(_$log_){
                $log = _$log_;
            }));

            $log.info('it worked!');
            expect($log.info.logs).toContain(['it worked!']);
        });
    });

}())

这在注入行上失败并出现错误:

TypeError: Cannot set property 'typeName' of undefined

我错过了什么?

1 个答案:

答案 0 :(得分:12)

这是怎么回事:

describe('basic test', function(){
    var log;
    beforeEach(inject(function(_$log_){
        log = _$log_;
    }));

    it('should just work', function(){
        log.info('it worked!');
        expect(log.info.logs).toContain(['it worked!']);
    });
});