我正在尝试使用Jasmine测试一些Typescript类。我创建了一些通过模块声明相互组合的类:
module xxx.DeviceData {
export class ConsoleListener extends DataListener {
constructor() {
super("ConsoleListener");
}
addData(data: string) {
console.log(this.title + ": " + data);
}
clear() {
console.clear();
}
}
}
我以同样的方式组成了Jasmine测试:
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());
});
});
}
这一切都完美无缺。当我尝试执行测试时,我收到此错误
未捕获的TypeError:对象原型可能只是一个Object或null:在Scripts / 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",
},
});
};
答案 0 :(得分:3)
当你从超类继承时,错误看起来像来自extends
函数TypeScript注入。
查看代码时,我会说您使用时无法使用DataListener
:
extends DataListener
它可能不会完全丢失,否则编译器会警告你 - 所以当Jasmine运行时它或者没有被包含(即加载),或者你的东西没有按顺序加载。
让他们井井有条,希望......快乐!
files: [
'Scripts/DeviceData/DataListener.ts',
'Scripts/DeviceData/ConsoleListener.ts',
'Scripts/UnitTests/*.spec.ts'
],
答案 1 :(得分:2)
TypeScript编译器生成__extends()
function来处理类继承。 b
表示基类,d
表示派生类。所以,问题是缺少基类DataListener
。检查编译和捆绑脚本的方式。可以在多个文件中定义一个名称空间(参见module
关键字),但必须手动或使用compiler option --outFile
处理其收集/捆绑。