我正尝试在 karma 中为 Angular 应用程序构建一些测试,并且我正在涉足 ES6。我正在尝试使用箭头函数而不是旧格式,但是当我尝试将具有多个参数的函数传递给注入(inject)函数时出现错误。
这很好用:
it('this is a test', () => {
.....
});
这会抛出“TypeError:无法读取 null 的属性‘1’”:
it('this is another test',inject(($state, $q, $httpBackend)=> {
....
});
虽然这很好用:
it('this is another test',inject(function($state, $q, $httpBackend) {
....
});
有没有想过为什么会这样?非常感谢。
最佳答案
对于遇到此问题的其他人(https://xkcd.com/979),Angular(<1.5)注入(inject)与 ES6 箭头函数不兼容。升级或使用普通函数进行注入(inject)/模块。
它显然依赖于 toString 来获取参数名称,并且被箭头函数破坏了:/https://stackoverflow.com/a/32700361
关于javascript - ES6箭头函数TypeError : Cannot read property '1' of null with multiple params using Karma,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32912323/