我知道这很长,但我需要在主要问题之前提供一些背景知识.我正在创建一个将分为两列的页面.这是ui-router代码:
$urlRouterProvider.otherwise("/projects/edit")
$stateProvider
.state('projects', {
url: "/projects",
template: "<div ui-view></div>"
})
.state('projects.edit', {
resolve: {
test: function(){
console.log('projects.edit resolve called')
}
},
url: "/edit",
templateUrl: "projects.html",
controller: function($scope, $state){
$state.go('projects.edit.surveys')
$scope.transitionToOtherRoute = function () {
$state.transitionTo('otherRoute')
}
}
})
.state('projects.edit.surveys', {
resolve: {
items: function(){
var randomNumberArray = [1,2,3,4,5,6]
//this will just shuffle the array
for(var j, x, i = randomNumberArray.length; i; j = Math.floor(Math.random() * i), x = randomNumberArray[--i], randomNumberArray[i] = randomNumberArray[j], randomNumberArray[j] = x);
return randomNumberArray
}
}, …Run Code Online (Sandbox Code Playgroud) 我们正在使用ui-router 0.2.10.
我将一个resolve对象作为参数注入我的控制器,然后在控制器中设置一个范围变量.它在应用程序上完美运行如下:
国家提供者
$stateProvider.state('myState', {
resolve:{
foo: function(){
return 'bar';
},
url: '/',
templateUrl: 'index.html',
controller: 'FooCtrl'
})
Run Code Online (Sandbox Code Playgroud)
调节器
app.Controllers.controller('FooCtrl', ['$scope', '$state', 'foo',
function ($scope, $state, $log, Zone, foo) {
$scope.testVar = foo
console.log($scope.testVar);
}])
Run Code Online (Sandbox Code Playgroud)
然后,按照预期在Chrome中将"条形图"记录到控制台.
但是当使用Karma运行测试时,解析对象现在是未定义的,这使测试失败.这是测试代码:
describe('controllers', function(){
var $rootScope,
$scope,
$state
beforeEach(module('app'))
beforeEach(inject(function($injector) {
$state = $injector.get('$state')
$rootScope = $injector.get('$rootScope')
$scope = $rootScope.$new()
$controller = $injector.get('$controller')
}))
it('FooCtrl should exist', inject( function() {
$state.go('myState')
$rootScope.$apply()
$controller = $controller('FooCtrl', {
'$scope': $scope
})
$rootScope.$apply()
assert.equal($scope.testVar, "bar", "these strings …Run Code Online (Sandbox Code Playgroud)