Ankush Verma
Ankush Verma

Reputation: 43

Error in Unit testing help me to solve this

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.

Upvotes: 1

Views: 952

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328594

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

Upvotes: 1

Related Questions