我有一个 angularjs 网络应用程序,想在其中使用 qunit 进行单元测试。我有一个 Controller :
function RootCtrl($scope, $rootScope, $window, $location) {
// logger is empty at the start
$scope.logger = '';
// we have no login error at the start
$scope.login_error = '';
//
// Get values array of object
//
$rootScope.values = function (obj) {
var vals = [];
for( var key in obj ) {
if(key !== '$$hashKey' && key !== 'checked')
vals.push(obj[key]);
}
return vals;
}
}
现在我想用 qunit
为 values
函数编写单元测试。我将所有 js
文件都包含到 test/index.html
和 qunit.css
中。现在我的 test.js
有以下内容:
var injector = angular.injector(['ng', 'myApp']);
var init = {
setup : function () {
this.$scope = injector.get('$rootScope').$new();
}
}
module('RootCtrl', init);
test('RootCtrl', function(){
var $controller = injector.get('$controller');
$controller('RootCtrl', {
$scope : this.$scope,
$location : this.$location
});
equal(['value'], $controller.values({'key' : 'value'}))
});
但我收到错误:http://docs.angularjs.org/error/ $injector/unpr?p0=$rootElementProvider%20%3C-%20$rootElement%20%3C-%20$location%20%3C-%20$route at:
$controller('RootCtrl', {
$scope : this.$scope,
$location : this.$location
});
如何正确注入(inject)controller并使用$scope
, $rootScope
, $location
和其他服务?
谢谢。
最佳答案
试试这个而不是你的 Controller
$controller('RootCtrl',['$scope', '$rootScope', '$location','$route', function ($scope, $rootScope, $location, $route) {
$scope : this.$scope,
$location : this.$location
}]);
关于Angularjs 和 qunit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22008531/