Inject $log service on simple Jasmine test

2019-05-28 02:17发布

问题:

Yes, I'm completely new to Angular and Jasmine, and I can't figure out how to inject a mock $log for my test. This is the test:

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

}())

This fails on the inject line with an error:

TypeError: Cannot set property 'typeName' of undefined

What am I missing?

回答1:

How's this:

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