最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: 'undefined' is not a constructor (evaluating 'new JdRes()) - Stack Overflow

programmeradmin2浏览0评论

I am writing jasmine test spec for an angular controller. Here, I get the error TypeError: 'undefined' is not a constructor (evaluating 'new JdRes()) - though I've defined it as

JdRes = jasmine.createSpy('JdRes');

The code segment in the controller is as follows

function (myService, $scope, $attrs, $q, $parse) {
    'use strict';

    var JdRes, resource;

    JdRes = myService('JdRes');
    resource = new JdRes();
}

I am writing jasmine test spec for an angular controller. Here, I get the error TypeError: 'undefined' is not a constructor (evaluating 'new JdRes()) - though I've defined it as

JdRes = jasmine.createSpy('JdRes');

The code segment in the controller is as follows

function (myService, $scope, $attrs, $q, $parse) {
    'use strict';

    var JdRes, resource;

    JdRes = myService('JdRes');
    resource = new JdRes();
}
Share Improve this question edited Apr 14, 2015 at 20:16 Richard 3,38632 silver badges41 bronze badges asked May 25, 2014 at 23:49 user2099863user2099863 1211 gold badge1 silver badge3 bronze badges 1
  • 1 Why is there no closing } in your function? – Joeytje50 Commented May 26, 2014 at 0:04
Add a comment  | 

2 Answers 2

Reset to default 9

Based on the information you've provided, the only conclusion I can make is that jasmine.createSpy('JdRes') returns undefined.

That means that either jasmine.createSpy doesn't have a return statement, or it tries to return something that has a value of undefined. You should check if the function does indeed have a return statement, and if it does, its returned value is not undefined. There's nothing further I can tell you.

This will occur also when you inject a different number of items than the number of arguments to the function - either way, I believe. For example:

(function () {
'use strict';

angular.module('controllers').controller('myController', MyController);

MyController.$inject = ['$scope',
    '$state',
    '$compile',
    'aService',
    'aServiceNotDefinedInConstructorArgs'];

function MyController('$scope',
    '$state',
    '$compile',
    'aService') {

    var vm = this;
    ...
}

Here the difference is aServiceNotDefinedInConstructorArgs is being injected but is not argument to MyController.

发布评论

评论列表(0)

  1. 暂无评论