如何在Angular规范中注入$ routeProvider

时间:2016-10-20 18:07:21

标签: angularjs rspec

将包含$ routeProvider的此函数传递给模块定义时,如何在规范中正确模拟/注入?

module.js

Map#setPaintProperty

myModuleCtrl.js

angular.module('myModule', [
  // Without the function($routeProvider) below the test passes. With it, it fails.
  function($routeProvider) {
    $routeProvider.when('/some/url/:id', {templateUrl: 'template.html', reloadOnSearch: false});
  }
])

myModuleCtrl.spec.js

angular.module('myModule')
  .controller('myModuleCtrl', [
    '$scope',
    function ($scope) {
      $scope.testMethod = function () {
        alert('Test Me!');
      }
    }
  ]);

如在module.js中所述,没有$ routeProvider行,规范通过。有了它,它失败并显示以下消息:

describe('myModuleCtrl', function () {
  var controller;
  var $scope;

  beforeEach(angular.mock.module('myModule'));
  beforeEach(function () {
    $scope = {};
  });
  beforeEach(angular.mock.inject(function ($rootScope, $controller) {
    controller = $controller('myModuleCtrl', {'$scope': $scope});
  }));

  describe('when doing stuff', function() {
    it('does other stuff', function() {
      $scope.testMethod();
    });
  });
});

在spec文件中需要做什么才能加载此模块(包括$ routeProvider)?

1 个答案:

答案 0 :(得分:1)

您的模块应该注入依赖项ngRoute

beforeEach(angular.mock.module('myModule',['ngRoute']));

测试应该有相同的东西,

{{1}}