【发布时间】:2017-04-07 09:04:10
【问题描述】:
我正在尝试进行一项小型测试工作,以验证控制器是否已定义。
我收到的错误是:
myApp.orders module Order controller should .... FAILED
Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- OrdersCtrl
读取类似的错误与依赖关系有关,但我不知道是什么问题。
控制器:
'use strict';
angular.module('myApp.orders', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/orders', {
templateUrl: 'orders/orders.template.html',
controller: 'OrdersCtrl'
});
}])
.controller('OrdersCtrl', function($scope, $location) {
$scope.changeView = function(view){
$location.path(view); // path not hash
}
});
测试:
'use strict';
describe('myApp.orders module', function() {
beforeEach(module('myApp.orders'));
describe('Order controller', function(){
it('should ....', inject(function($controller) {
//spec body
var OrdersCtrl = $controller('OrdersCtrl');
expect(OrdersCtrl).toBeDefined();
}));
});
});
【问题讨论】:
-
应该不是这样,但是你可以试试 .controller('OrdersCtrl', ['$scope','$location',function($scope, $location) { $scope. changeView = function(view){ $location.path(view); // 路径不是哈希 } }]);
标签: angularjs unit-testing inject