简体   繁体   English

Angular Karma Test在$ scope。$ parent函数上获取“TypeError:'undefined'不是函数”

[英]Angular Karma Test getting “TypeError: 'undefined' is not a function” on a $scope.$parent function

While running grunt test on a Yeoman (1.0RC1) scaffolded Angular (1.0.7) app, I'm getting the following error: 在Yeoman(1.0RC1)脚手架Angular(1.0.7)应用程序上运行grunt test时,我收到以下错误:

TypeError: 'undefined' is not a function (evaluating '$scope.$parent.userLoggedIn(true)')

userLoggedIn() is within a parent controller index.js . userLoggedIn()在父控制器index.js The function itself runs fine within the angular app. 该功能本身在角度应用程序中运行良好。

This error doesn't occur on my other $scope.$parent boolean or strings variables in the controller, so it's related directly to calling functions within a parent. 我的其他$scope.$parent boolean或控制器中的字符串变量不会发生此错误,因此它与父级内的调用函数直接相关。

I'm thinking that I'm either using $scope.$parent the wrong way or that I need to define my index.js controller in the test, but Karma testing documentation is sporadic, so it's hard to know. 我想我要么使用$scope.$parent是错误的方式,或者我需要在测试中定义我的index.js控制器,但是Karma测试文档是零星的,所以很难知道。

EDIT: Here is the controller: 编辑:这是控制器:

'use strict';

angular.module('uiApp')
  .controller('LookupCtrl', ['$scope', function ($scope) {
    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];

    $scope.$parent.userLoggedIn(true);

  }]);

Here is the test: 这是测试:

'use strict';

describe('Controller: LookupCtrl', function () {

  // load the controller's module
  beforeEach(module('uiApp'));

  var LookupCtrl,
    scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope) {
    scope = $rootScope.$new();
    LookupCtrl = $controller('LookupCtrl', {
      $scope: scope
    });
  }));

  it('should attach a list of awesomeThings to the scope', function () {
    expect(scope.awesomeThings.length).toBe(3);
  });

});

(Yes I know I'm just using the default awesomeThings test for now. I'm new to Angular testing). (是的,我知道我现在只使用默认的awesomeThings测试。我是Angular测试的新手)。

You're giving the controller a rootScope , which doesn't have $parent (it's the root). 你给控制器一个rootScope ,它没有$parent (它是root)。 Change your controller code to call it correctly (using the prototype chain) and you'll be fine by just passing {my: props} as an object. 更改您的控制器代码以正确调用它(使用原型链),只需将{my: props}作为对象传递即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular Karma测试得到“ TypeError:&#39;undefined&#39;不是一个函数” - Angular Karma Test getting “TypeError: 'undefined' is not a function” AngularJS $ scope。$ watch回调函数参数在Jasmine测试中未定义 - AngularJS $scope.$watch callback function parameter undefined in Jasmine test Angularjs TypeError: <serviceName> 。 <functionName> 不是Scope。$ scope的函数。 <functionName> - Angularjs TypeError: <serviceName>.<functionName> is not a function at Scope.$scope.<functionName> $ scope。$ watch(var,function)未定义不是函数 - $scope.$watch(var,function) undefined is not a function $ scope。$ apply返回未定义的错误不是函数 - $scope.$apply return error of undefined is not a function TypeError 业力测试:订阅不是 function - TypeError karma test : subscribe is not a function 在$ scope。$ watch函数内不断收到“ TypeError:无法读取null的属性&#39;value&#39;” - Keep getting “TypeError: Cannot read property 'value' of null” inside of $scope.$watch function 角$ scope不应用在回调函数中,即使使用$ scope。$ apply - Angular $scope not applying in callback function, even with $scope.$apply Angular Karma Jasmine - 测试功能 - Angular Karma Jasmine - test function angularjs $ setPristine得到一个错误:TypeError:$ scope。$ setPristine不是一个函数 - angularjs $setPristine get an error: TypeError: $scope.$setPristine is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM