簡體   English   中英

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

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

在Yeoman(1.0RC1)腳手架Angular(1.0.7)應用程序上運行grunt test時,我收到以下錯誤:

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

userLoggedIn()在父控制器index.js 該功能本身在角度應用程序中運行良好。

我的其他$scope.$parent boolean或控制器中的字符串變量不會發生此錯誤,因此它與父級內的調用函數直接相關。

我想我要么使用$scope.$parent是錯誤的方式,或者我需要在測試中定義我的index.js控制器,但是Karma測試文檔是零星的,所以很難知道。

編輯:這是控制器:

'use strict';

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

    $scope.$parent.userLoggedIn(true);

  }]);

這是測試:

'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);
  });

});

(是的,我知道我現在只使用默認的awesomeThings測試。我是Angular測試的新手)。

你給控制器一個rootScope ,它沒有$parent (它是root)。 更改您的控制器代碼以正確調用它(使用原型鏈),只需將{my: props}作為對象傳遞即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM