Angular.js: Function.prototype.bind.apply(...) is not a constructor

Created on 22 Jun 2016  ·  2Comments  ·  Source: angular/angular.js

Im trying to learn angular-tdd

with this versions my tests is working fine
depencies version
angular: 1.4.8
angular-mocks: 1.4.8
node: v4.4.5
karma-cli: 0.13.22

but when i use
versions
"angular": "^1.5.7"
"angular-mocks": "^1.5.7"

it throws an error

karma start karma.conf.js

22 06 2016 16:53:04.002:WARN [karma]: No captured browser, open http://localhost:9876/
22 06 2016 16:53:04.012:INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
22 06 2016 16:53:04.018:INFO [launcher]: Starting browser Chrome
22 06 2016 16:53:05.959:INFO [Chrome 51.0.2704 (Linux 0.0.0)]: Connected on socket /#Vfy_Fpt6BZ2INRyEAAAA with id 36097936
Chrome 51.0.2704 (Linux 0.0.0) calculator sum 1 + 1 should equal 2 FAILED
    TypeError: Function.prototype.bind.apply(...) is not a constructor
        at Object.instantiate (/home/malanay/workspace/AngularJs-BDD-101/node_modules/angular/angular.min.js:41:477)
        at /home/kiro112/workspace/AngularJs-BDD-101/node_modules/angular/angular.min.js:90:3
        at /home/kiro112/workspace/AngularJs-BDD-101/node_modules/angular-mocks/angular-mocks.js:2214:12
        at Object.it (/home/kiro112/workspace/AngularJs-BDD-101/tests/calculator.controller.test.js:17:21)
Chrome 51.0.2704 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.027 Chrome 51.0.2704 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.037 secs / 0.027 secs)
invalid

Most helpful comment

The problem is using the fat arrow syntax to define the function constructor for the controller class. Assuming your function passed as the second argument to module.controller is Foo, Angular will call new Foo(...) on it, which is illegal with the fat arrow syntax in ES6.

All 2 comments

The problem is using the fat arrow syntax to define the function constructor for the controller class. Assuming your function passed as the second argument to module.controller is Foo, Angular will call new Foo(...) on it, which is illegal with the fat arrow syntax in ES6.

@wesleycho, is right. Controllers should be instantiable (i.e. called with new), so things like arrow functions (() => {...}) and shorthand object methods ({someMethod() {...}}) aren't suitable for controllers.

See https://github.com/angular/angular.js/issues/14766#issuecomment-226005516 for more details.

Was this page helpful?
0 / 5 - 0 ratings