我的Angular项目中有两个管道。两者的测试看起来相同,只是存在性的测试。其中之一失败并显示错误消息:An error was thrown in afterAll\nUncaught TypeError: _this.handler.handle is not a function thrown
测试如下所示:
it('create an instance', () => {
const pipe = new MyPipe();
expect(pipe).toBeTruthy();
});
即使我将代码更改为甚至不创建Pipe,它仍然会失败。
it('create an instance', () => {
expect(true).toBeTruthy();
});
因此,测试本身似乎有问题,但我无法弄清原因。
karma.conf.js
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma'),
require('karma-junit-reporter')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, 'coverage'),
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true
},
reporters: config.angularCli && config.angularCli.codeCoverage
? ['progress', 'coverage-istanbul', 'junit']
: ['progress', 'kjhtml', 'junit'],
junitReporter: {
outputFile: 'test-results.xml'
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
答案 0 :(得分:0)
您应确保代码中符合以下条件:
有关此内容,请参见此处的SO帖子: TypeError: _this.handler.handle is not a function error
错误:
const printAge = function () {
console.log(this.age)
}
let human = new Human('male', 30)
human.printAge = printAge
human.printAge() // 30
右:
this.myservice.func().subscribe(result => {
// do stuff
})
有关该内容,请参见SO帖子: Angular5 / ng test ERROR : TypeError: this.handler.handle is not a function
(最佳答案) 请注意,如果您在ngInit中进行某种服务调用,则尤其如此。