Sony Khan
Sony Khan

Reputation: 1504

Angular: How to resolve [$injector:unpr] error for $httpProvider and inject controller in jasmine test

I am new to angular testing. Facing some issues while testing angular code using jasmine.

It will be highly appreciated if you read my question and try to solve my problem as i googled it but could not find any satisfactory solution

Here is my angular app

var app = angular.module('myApp', ['ngAnimate', 'ui.router', 'ui.bootstrap', 'toggle-switch',
    'ngTagsInput', 'blockUI', 'ngBootbox', 'ui.select', 'ngSanitize', 'angular.filter']);

app.config(["$httpProvider", "blockUIConfig", function ($httpProvider, blockUIConfig) {
    'use strict';
    blockUIConfig.autoBlock = false;
    $httpProvider.defaults.xsrfCookieName = 'csrftoken';
    $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
    $httpProvider.interceptors.push('interceptorService');
}]);

And here is controller file:

app.controller('myController', ['$scope', '$filter', '$http', '$window', '$ngBootbox', '$modal', 'dataservice', 'user', 'message_kinds',
    function($scope, $filter, $http, $window, $bB, $modal, dataservice, user, message_kinds) {
       $scope.user = user;
       /controller logic/
}]);

I want to test this controller if $scope.user equals to user or not.Am using jasmine for testing. Here is spec file.

describe("myController", function() {

  beforeEach(module('myApp'));

  beforeEach(inject(function(_$controller_){
    $controller = _$controller_;
  }));

  describe("myController testing", function () {

    it("should update scope.user", function () {
      var user = {customer_id: 1};
      var my_controller = $controller('myController', { user: user });
      expect(my_controller.user).toEqual(user);
    });
  });

});

I have also included all dependency files like angular.js, angular-mocks.js etc in SpecRunner.html

Having three problems:

  1. Facing [$injector:unpr] http://errors.angularjs.org/1.4.4/$injector/unpr?p0=interceptorServiceProvide error on app.config block regarding $httpProvider
  2. ReferenceError: $controller is not defined in spec.js at line

    var my_controller = $controller('myController', { user: user });

  3. How can I test if scope.user is equals to user in expect block?

Upvotes: 0

Views: 531

Answers (1)

rrd
rrd

Reputation: 5957

1) Check this answer, as regards $http/$httpBackend which might help you - you can adapt this to get the answers you're looking for

2) Have you declared $controller (and now $httpProvider) as a variable in the beginning of the describe() block?

3) You should have that already. Your code, at least as I can see, looks like it should work like you want it to.

Upvotes: 1

Related Questions