1

I got an error in this test. Can anyone tell me why this happen?

describe('Controller: LoginCtrl', function () {
  beforeEach(function(){

    var $httpBackend;
    module("loginModule");
  });

  beforeEach(inject(function($controller, $rootScope, $location, $auth, $httpBackend) {
    this.$location= $location;
    this.$httpBackend=$httpBackend;
    this.scope= $rootscope.$new();
    this.redirect = spyOn($location,'path');

    $controller('LoginCtrl',{
        $scope: this.scope,
        $location: $location,
        auth: $auth

    });

  }));



  describe("succesfully loggin in",function(){
    it("should redirect you to home",function(){
        //arrange
        $httpBackend.expectPOST('/login',scope.login).respond(200);
        //act
        scope.login();
        $httpBackend.flush();

        //assertion
        expect(this.redirect).toHaveBeenCalledWith('/login');
    });

  });
});

The error I got is:

Error: [$injector:unpr] Unknown provider: $authProvider <- $auth
http://errors.angularjs.org/1.2.28/$injector/unpr?p0=%24authProvider%20%3C-%20%24auth
    at C:/xampp/htdocs/app/bower_components/angular/angular.js:3801
    at getService (C:/xampp/app/bower_components/angular/angular.js:3929)
    at C:/xampp/htdocs/app/bower_components/angular/angular.js:3806
    at getService (C:/xampp/htdocs/app/bower_components/angular/angular.js:3929)
    at invoke (C:/xampp/htdocs/app/bower_components/angular/angular.js:3956)
    at workFn (C:/xampp/htdocs/app/bower_components/angular-mocks/angular-mocks.js:2177)
undefined
ReferenceError: Can't find variable: $httpBackend
    at C:/xampp/htdocs/test/spec/controllers/login.js:32
PhantomJS 1.9.8 (Windows 8): Executed 1 of 1 (1 FAILED) ERROR (0.02 secs / 0.021 secs)
Warning: Task "karma:unit" failed. Use --force to continue.
Daniel Olszewski
  • 13,995
  • 6
  • 58
  • 65
  • The error specifies that $auth is not available in the module. What are you trying to do with $auth in the controller? You might just be able to remove all references to it. – Peter Ashwell Dec 29 '14 at 09:18

1 Answers1

1

Angular provides automatic mocks for many things ($controller, $httpBackend) but not for $auth. You need to implement your own $authProvider (see https://docs.angularjs.org/guide/providers for a general overview) or mock $auth directly.

Use $provide to let Angular know about your new custom mock.

Related: Injecting a mock into an AngularJS service

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820