使用$ rootScope进行角度单元测试。$ on(" name")

时间:2014-10-12 04:39:26

标签: angularjs unit-testing

使用Mocha和Jasmine,我正在尝试对此服务进行单元测试:

angular.module("main.loadbalancer").run(function($rootScope, $state, DeviceVal) {
  $rootScope.$on("$stateChangeStart", function(event, toState) {
    var checkUrlDirectEdit, device;
    device = window.location.hash.split("/")[2];
    checkUrlDirectEdit = function() {
      return DeviceVal.previousDevice !== device && toState.name !== "main" && DeviceVal.lb.id === "undefined";
    };
    if (typeof DeviceVal.lb.id === "undefined" && toState.name !== "main" || checkUrlDirectEdit()) {
      event.preventDefault();
      DeviceVal.previousDevice = device;
      $state.transitionTo("main", {}, {
        location: false
      });
    }
    if (DeviceVal.readonly && toState.name !== "main.loadbalancer.readonly") {
      event.preventDefault();
      $state.transitionTo("main.loadbalancer.readonly", {
        id: DeviceVal.lb.id
      }, {
        location: true,
        reload: true
      });
    }
    return window.addEventListener("hashchange", function() {
      return location.reload();
    });
  });
});

通过这个测试:

describe("health check service", function() {
  var HealthCheckSvc;
  HealthCheckSvc = null;

  beforeEach(function() {
    return module("main.loadbalancer");
  });

  beforeEach(inject(function($rootScope, _$state_, _DeviceVal_) {
    this.$rootScope = $rootScope;
    $state = _$state_;
    $DeviceVal = _DeviceVal_;

    spyOn($state, "transitionTo")
    spyOn(this.$rootScope, "$on")


  }));

  it("do some junk", function() {
      toState = 'main.loadbalancer.nodes';
      this.$rootScope.$broadcast("$stateChangeStart", "event", "toState");
      expect(this.$rootScope.$on).toHaveBeenCalledWith("$stateChangeStart");

    });
});

现在,我很难对$rootScope.$on("$stateChangeStart")做出期待。我认为spyOn(this.$rootScope, "$on")expect(this.$rootScope.$on).toHaveBeenCalledWith("$stateChangeStart");会这样做,但我得到了:

 Expected spy $on to have been called with [ '$stateChangeStart' ] but it was never called.
        Error: Expected spy $on to have been called with [ '$stateChangeStart' ] but it was never called.

2 个答案:

答案 0 :(得分:2)

$rootScope.$on("$stateChangeStart"称为早期,只是注册事件监听器

当您致电this.$rootScope.$broadcast("$stateChangeStart", "event", "toState");时,$rootScope.$on未被调用,但处理程序内的代码function(event, toState) {

答案 1 :(得分:1)

有可能,但我怀疑在运行run块之后添加了间谍。解决这个问题的方法是将自己的模拟$rootScope对象与存根/间谍$on函数一起提供给依赖注入系统,您可以在run运行之前执行此操作。< / p>

describe('run block', function() {
  var $rootScope = null;

  beforeEach(module('plunker'));

  beforeEach(module(function($provide) {
    $provide.value('$rootScope', {
      '$on': jasmine.createSpy('$on')
    });
  }));

  beforeEach(inject(function(_$rootScope_) {
    $rootScope = _$rootScope_;
  }));

  it('should call $on with $stateChangeStart and a function', function() {
    expect($rootScope.$on).toHaveBeenCalledWith('$stateChangeStart', jasmine.any(Function));
  });
});

可以看到http://plnkr.co/edit/XovukFUxOY479muQu4WW


补充工具栏:我不确定您是否需要此测试。我认为确保它在事件发生时应该做的事情更重要。