[英]AngularJS Karma test - Resolve object passed into controller is undefined when testing
We are using ui-router 0.2.10. 我们正在使用ui-router 0.2.10。
I am injecting a resolve object as a parameter into my controller, which is then setting a scope variable in the controller. 我将一个resolve对象作为参数注入我的控制器,然后在控制器中设置一个范围变量。 It works perfectly on the app like so:
它在应用程序上完美运行如下:
state provider 国家提供者
$stateProvider.state('myState', {
resolve:{
foo: function(){
return 'bar';
},
url: '/',
templateUrl: 'index.html',
controller: 'FooCtrl'
})
controller 调节器
app.Controllers.controller('FooCtrl', ['$scope', '$state', 'foo',
function ($scope, $state, $log, Zone, foo) {
$scope.testVar = foo
console.log($scope.testVar);
}])
'Bar' is then logged to the console as expected in Chrome. 然后,按照预期在Chrome中将“条形图”记录到控制台。
But when running tests using Karma, the resolve object is now undefined, which fails the test. 但是当使用Karma运行测试时,解析对象现在是未定义的,这使测试失败。 Here is the test code:
这是测试代码:
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 are equal")
}))
})
This error is presented (the resolve object in my case is called resolvedRouteModels): 出现此错误(在我的情况下,resolve对象称为resolvedRouteModels):
[$injector:unpr] Unknown provider: fooProvider <- foo
http://errors.angularjs.org/1.3.0-build.2921+sha.02c0ed2/$injector/unpr?p0=fooProvider%20%3C-%20foo
Any help would be much appreciated, and please let me know if you have encountered this problem. 任何帮助将不胜感激,如果您遇到此问题,请告诉我。
When you instantiate your controller, Angular usually can figure out how to satisfy the controller's dependencies. 当您实例化控制器时,Angular通常可以弄清楚如何满足控制器的依赖关系。 In this case, it doesn't know about UI-Router's "resolve" functionality.
在这种情况下,它不知道UI-Router的“解析”功能。
One way to address this is to supply this dependency yourself in the test, the same way you are passing in the scope to the controller: 解决此问题的一种方法是在测试中自己提供此依赖项,就像将范围传递给控制器一样:
var foo = 'bar'; // whatever
$controller = $controller('FooCtrl', {$scope: $scope, foo: foo} );
Note, you could also create a mock $state
object and pass that into the controller the same way, if you wanted to incorporate that into your tests. 注意,您还可以创建一个模拟
$state
对象,并以相同的方式将其传递给控制器,如果您想将其合并到测试中。
my assumption is your Angular set up is perfect, if that's the case, you might want to test your code this way. 我的假设是你的Angular设置是完美的,如果是这样的话,你可能想用这种方式测试你的代码。 I've used Jasmine 2 syntax.
我使用了Jasmine 2语法。
describe('Foo Controller', function() {
var $scope, $state, controller, Zone, foo, $log;
beforeEach(module('app'));
beforeEach(inject(function($controller) {
$scope = {};
$state = {};
$log = {};
Zone = {};
foo = {};
controller = $controller;
}));
it('should log the value foo', function() {
spyOn(console, 'log');
controller('FooCtrl', { $scope, $state, $log, Zone, foo });
expect($scope.testVar).toEqual({});
expect(console.log).toHaveBeenCalledWith({});
});
it('should log the value foo', function() {
spyOn(console, 'log');
// You could change the value of foo i.e.
foo = 'create more spies than fbi';
controller('FooCtrl', { $scope, $state, $log, Zone, foo });
expect($scope.testVar).toEqual('create more spies than fbi');
expect(console.log).toHaveBeenCalledWith('create more spies than fbi');
});
});
Once again I hope this helps. 我再次希望这会有所帮助。 Peace.
和平。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.