TypeError: 'undefined' is not a constructor (evaluating 'new JdRes())

47,116

Solution 1

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.

Solution 2

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.

Share:
47,116
user2099863
Author by

user2099863

Updated on July 23, 2022

Comments

  • user2099863
    user2099863 almost 2 years

    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();
    }
    
  • Allyl Isocyanate
    Allyl Isocyanate over 8 years
    For posterity, this behavior also occurs if you do spyOn(obj, 'someMethod').and.return('some value'). It should be .and.returnValue('some value')