Using $injector in AngularJS when integration test

2019-04-11 04:34发布

问题:

I'm needing to setup some integration tests in AngularJS using Karma/Jasmine but having trouble, because when not using ngMock (since I want to hit the actual $http endpoints), there are no module or inject methods.

So how do I inject services into my tests?

I've tried angular.injector.invoke(...) but can't get it working, always comes back with an error like Unknown provider: AuthServiceProvider <- AuthService.

Thoughts?

回答1:

Try this

'use strict';

describe('Login User', function () {
    var app, LoginService;

    beforeEach(module('app')) ;

    beforeEach(inject(function(_LoginService_) {
        LoginService = _LoginService_;
    })) ;

   it('Should be logged in', function () {
       var isLoggedIn = LoginService.isUserLoggedIn();
       expect(isLoggedIn).toBeTruthy();
    });
 });