簡體   English   中英

$ routeUpdate無法在角度業力單元測試中廣播?

[英]$routeUpdate not broadcast in angular karma unit test?

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測試

    '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();
  });
});

運行測試時,“我被稱為”從不記錄。

也許您應該自己廣播事件,然后測試該事件的處理方法:

it('do call', function () {

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

    expect....
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM