How to inject controller dependencies in Jasmine tests?

六眼飞鱼酱① 提交于 2019-12-05 19:41:11

The services should be automatically injected. If you wish to mock them or spy on them, inject them like so:

describe('HomeController', function() {
  beforeEach(module('app'));

  var $controller, $scope, $modal, Point;

  beforeEach(inject(function(_$controller_, _$rootScope_, _$modal_, _Point_){
    $scope = $rootScope.$new();
    $modal = _$modal_;
    Point = _Point_;

    spyOn($modal, 'method');
    spyOn(Point, 'method');

    $controller = _$controller_('HomeController', { $scope: $scope, $modal: $modal, Point: Point });
  }));

  describe('$scope.grade', function() {
    it('sets the strength to "strong" if the password length is >8 chars', function() {
      $scope.label = '12345';
      $scope.addNewPoint();
      expect($scope.label).toEqual(null);
    });
  });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!