简体   繁体   English

AngularJS Karma测试 - 测试时未定义传递给控制器​​的Resolve对象

[英]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.

相关问题 在Karma中为angularjs测试控制器时的模拟服务 - Mocking service when testing controller in Karma for angularjs TypeError:在使用Karma测试AngularJS控制器时无法读取未定义的属性&#39;then&#39; - TypeError: Cannot read property 'then' of undefined in testing AngularJS controller with Karma 使用Karma解决的AngularJS 1.x组件测试有望测试成功功能(spyOn) - AngularJS 1.x Component testing with Karma resolve promise to test success function (spyOn) 解决Karma单元测试中的嵌套AngularJS承诺 - Resolve nested AngularJS promises in Karma unit test 使用业力测试angularjs时初始化控制器时出错 - Error initializing controller while testing angularjs with karma Karma / Jasmine没有找到AngularJS控制器(说它未定义) - Karma / Jasmine not finding AngularJS controller (says it is undefined) 如何使用Karma在AngularJS控制器中测试$元素? - How to test $element in AngularJS controller using Karma? Angularjs&Karma控制器单元测试喷射器:modulerr - Angularjs & Karma controller unit test injector:modulerr 用于茉莉/业力测试的angularjs中的全局模拟对象 - Globally mock object in angularjs for jasmine/karma testing 使用业力在angularjs中进行测试时得到``未定义需求&#39;&#39; - getting 'require is not defined' when testing in angularjs with karma
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM