我正在努力获得使用Jasmine测试的一些Typescript类。我创建了一些类,通过模块的声明相互组成:这在茉莉花测试中意味着什么:Uncaught TypeError:对象原型可能只是一个Object或null:undefined?
module xxx.DeviceData {
export class ConsoleListener extends DataListener {
constructor() {
super("ConsoleListener");
}
addData(data: string) {
console.log(this.title + ": " + data);
}
clear() {
console.clear();
}
}
}
我撰写的茉莉花测试一起以同样的方式:
module xxx.DeviceData {
describe('ConsoleListener test', function() {
var consoleListener,
consoleListenerObj;
beforeEach(() => {
consoleListener = jasmine.createSpy("consoleListener");
consoleListenerObj = jasmine.createSpyObj('consoleListenerObj', ['onSuccess', 'onFailure', 'addData', 'clear']);
consoleListenerObj.onSuccess();
consoleListenerObj.onFailure();
consoleListenerObj.addData();
consoleListenerObj.clear();
});
it('test#1 columnDefinition defined', function() {
let consoleListener = new ConsoleListener();
expect(consoleListener).not.toBeNull();
});
it('test#2 call onSuccess', function() {
expect(consoleListenerObj.onSuccess).toHaveBeenCalled();
});
it('test#3 call onFailure', function() {
expect(consoleListenerObj.onFailure).toHaveBeenCalled();
});
it('test#4 call addData', function() {
expect(consoleListenerObj.addData('123'));
});
it('test#5 call clear', function() {
expect(consoleListenerObj.clear());
});
});
}
这一切都完美transpiles。当我尝试执行测试,我收到此错误
遗漏的类型错误:对象的原型可能只是一个对象或null:在脚本/ DeviceData/ConsoleListener.js未定义:5:27
事情错了在传送的JS的第5行,对不对?这里是:
var __extends = (this && this.__extends) || (function() {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var xxx;
(function (xxx) {
var DeviceData;
(function (DeviceData) {
var ConsoleListener = (function (_super) {
__extends(ConsoleListener, _super);
function ConsoleListener() {
return _super.call(this, "ConsoleListener") || this;
}
ConsoleListener.prototype.addData = function (data) {
console.log(this.title + ": " + data);
};
ConsoleListener.prototype.clear = function() {
console.clear();
};
return ConsoleListener;
}(DeviceData.DataListener));
DeviceData.ConsoleListener = ConsoleListener;
})(DeviceData = xxx.DeviceData || (xxx.DeviceData = {}));
})(xxx|| (xxx= {}));
//# sourceMappingURL=ConsoleListener.js.map
当然,第5行似乎是在谈论对象和原型。
我试过不同的方式让模块彼此交谈,但是这个模块方法是我能够始终如一地工作的唯一方法。需要在这里传递的业力/茉莉花背景中是否缺少某些东西?
这里是我的karma.config:
module.exports = function (config) {
config.set({
frameworks: ["jasmine","karma-typescript"],
preprocessors: {
"Scripts/**/*.ts": ["karma-typescript"]
},
files: [
'Scripts/DeviceData/*.ts',
'Scripts/UnitTests/*.spec.ts'
],
exclude: [
'Scripts/**/NodeJSDataSocket.ts'
],
reporters: ["progress", "karma-typescript"],
//reporters: ["dots", "karma-typescript"],
browsers: ["Chrome"],
karmaTypescriptConfig: {
compilerOptions: {
"module": "commonjs",
"sourceMap": true,
"target": "es5"
"moduleResolution": "classic",
"noImplicitAny": false
},
tsconfig: "./tsconfig.json",
},
});
};
优秀 - 这在回顾过程中显得很明显。我的第一个想法是,它没有包含在karma.config中,但它应该是 - 我已经在上面发布了。任何其他想法为什么它不包括在内?与此同时,我有一个独立的文件可以分离和测试以确认这一点。 –
是的,这工作,所以它绝对是在那里继承。 –
哪个文件是'DataListener'中的? – Fenton