Typescript 这在jasmine测试中意味着什么:未捕获的TypeError:对象原型可能只是一个对象,或者null:未定义?
我正在用Jasmine测试一些Typescript类。我创建了一些通过模块声明相互组成的类:Typescript 这在jasmine测试中意味着什么:未捕获的TypeError:对象原型可能只是一个对象,或者null:未定义?,typescript,jasmine,karma-jasmine,Typescript,Jasmine,Karma Jasmine,我正在用Jasmine测试一些Typescript类。我创建了一些通过模块声明相互组成的类: module xxx.DeviceData { export class ConsoleListener extends DataListener { constructor() { super("ConsoleListener"); } addData(data: string) { consol
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:对象原型只能是对象或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",
},
});
};
当您从超类继承时,该错误似乎来自函数TypeScript injects的
extends
看看代码,我想说当您使用它时,DataListener
不可用:
extends DataListener
它可能并没有完全丢失,否则编译器会警告您——因此,当Jasmine运行时,它要么没有被包含(即加载),要么您加载的东西不正常
把它们整理好,希望。。。快乐
files: [
'Scripts/DeviceData/DataListener.ts',
'Scripts/DeviceData/ConsoleListener.ts',
'Scripts/UnitTests/*.spec.ts'
],
由TypeScript编译器生成以处理类继承
b
表示基类,d
表示派生类。因此,问题是缺少基类DataListener
。检查编译和捆绑脚本的方式。可以跨多个文件定义一个名称空间(参见module
关键字),但它们的收集/绑定必须手动或使用来处理。非常好-回顾起来,这一点很明显。我的第一个想法是它没有包含在karma.config中,但它应该包含在karma.config中——我已经在上面发布了。关于为什么不包括它还有其他想法吗?同时,我有一个独立的文件,我可以隔离并测试以确认这一点。是的,这是有效的,因此它肯定是继承的。哪个文件是DataListener
In?它与ConsoleListener位于同一个目录中,即Scripts/DeviceData.Aha!因此,它们(可能)是按字母顺序加载的,ConsoleListener
在DeviceData
之前加载,因此尽管您同时加载两个脚本,但实际上需要强制执行顺序,以确保您所依赖的类是第一个。