$ routeUpdate不在角度业力单元测试中广播?

时间:2015-03-25 01:08:58

标签: javascript angularjs unit-testing jasmine

app.js

  'use strict';

angular
  .module('scrCliApp', [
    'ngRoute',
    'ui.bootstrap'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/search', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl',
        reloadOnSearch: false
      })
      .otherwise({
        redirectTo: '/search'
      });
  });

main.js控制器

angular.module('scrCliApp')
  .controller('MainCtrl', function ($scope) {
    $scope.$on('$routeUpdate', function(/*scope, next, current*/) {
      console.log('i was called'); //never logs
    });
  });

main.js test

    'use strict';

describe('Controller: MainCtrl', function () {

  // load the controller's module
  beforeEach(module('scrCliApp'));

  var MainCtrl,
    scope,
    rootScope,
    location;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope, $location) {
    rootScope = $rootScope;
    location = $location;
    scope = $rootScope.$new();
    MainCtrl = $controller('MainCtrl', {
      $scope: scope
    });
  }));

  it('do call', function () {
    location.search('q', 'b');
    scope.$apply();
  });
});

运行测试时,“我被称为”永远不会记录。

1 个答案:

答案 0 :(得分:1)

也许你应该自己广播这个活动,然后测试一下它是怎么做的:

it('do call', function () {

    scope.$emit('$routeUpdate', [1,2,3]);
    scope.$apply();

    expect....
});
相关问题