Unit Test Controller with dependency on $ionicViewService


I’m using jasmine + grunt-karma for writing unit test. I’ve a controller which has $ionicViewService as dependency.

The issue encountered when unit testing the controller:

    TypeError: Cannot read property 'historyId' of null
 at Object.clearHistory (/../app/bower_components/ionic/release/js/ionic-angular.js:3376:23)
at new <anonymous> (/../app/bower_components/angular/angular.js:3906:17)

What could have caused this error ?

My Controller:

angular.module('cApp').controller('MyCtrl', function($scope, $ionicViewService) {

My Unit test:

describe('MyCtrl', function() {
  var scope, controller;

  // Load the module
  beforeEach(function () {

  beforeEach(inject(function($rootScope, $controller) {
    scope = $rootScope.$new();
    controller = $controller('MyCtrl', {
      $scope: scope

  it('should have the controller', inject(function() {


I just resolved it by referring to:

I added the following into beforeEach, and the error disappear.

viewService = $ionicViewService;
window = $window;
window.history.go = function (val) { return val; }

var uiViewScope = {};

I guess the viewService.register(…) is the one that resolve this.

-Issue Closed-