Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using $injector in AngularJS when integration testing (without using ngMock)

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?

like image 535
ngDeveloper Avatar asked Jul 19 '26 06:07

ngDeveloper


1 Answers

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();
    });
 });
like image 111
Martin Avatar answered Jul 21 '26 03:07

Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!