javascript - Angular Karma Test getting "TypeError: 'undefined' is not a function" on a $scope

While running grunt test on a Yeoman (1.0RC1) scaffolded Angular (1.0.7) app, I'm getting the foll

While running grunt test on a Yeoman (1.0RC1) scaffolded Angular (1.0.7) app, I'm getting the following error:

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

userLoggedIn() is within a parent controller 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.

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.

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).

While running grunt test on a Yeoman (1.0RC1) scaffolded Angular (1.0.7) app, I'm getting the following error:

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

userLoggedIn() is within a parent controller 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.

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.

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).

Share Improve this question edited Aug 22, 2013 at 19:39 Sartaj asked Aug 22, 2013 at 16:15 SartajSartaj 1181 gold badge1 silver badge9 bronze badges 2
  • Could you please provide your code for the testing controller. – BKM Commented Aug 22, 2013 at 16:20
  • In general that just means that you're trying to use a property name to find a function, but the object you're working with doesn't have such a property. Thus, you're trying to use undefined as a function, and it's not a function. – Pointy Commented Aug 22, 2013 at 16:21
Add a ment  | 

1 Answer 1

Reset to default 1

You're giving the controller a rootScope, which doesn't have $parent (it's the 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745312472a4622062.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信