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) {
  $ionicViewService.clearHistory();
  ...
}

My Unit test:

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

  // Load the module
  beforeEach(function () {
    module('cApp');
    module('ionic');
  });

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

  it('should have the controller', inject(function() {
    expect(controller).toBeDefined();
  }));
});

I just resolved it by referring to:
https://github.com/driftyco/ionic/blob/master/test/unit/angular/service/viewService.unit.js

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

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

var uiViewScope = {};
$state.go('home');
$rootScope.$apply();
viewService.register(uiViewScope);

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

-Issue Closed-