首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这在茉莉测试中意味着什么:未定义的TypeError:对象原型可能只是一个对象或null:未定义?

这在茉莉测试中意味着什么:未定义的TypeError:对象原型可能只是一个对象或null:未定义?
EN

Stack Overflow用户
提问于 2017-10-05 19:44:16
回答 2查看 1.5K关注 0票数 3

我在用茉莉花做一些打字稿的测试。我创建了一些通过模块声明相互组成的类:

代码语言:javascript
复制
module xxx.DeviceData {
    export class ConsoleListener extends DataListener {
        constructor() {
            super("ConsoleListener");
        }

        addData(data: string) {
            console.log(this.title + ": " + data);
        }

        clear() {
            console.clear();
        }
    }
}

我把茉莉花测试用同样的方式组合在一起:

代码语言:javascript
复制
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());
     });
 });
}

这一切都很完美。当我尝试执行测试时,我会收到这个错误。

Uncaught : Object prototype可能只是一个对象或null:未定义于Script/DeviceData/ConsoleListener.js:5:27

JS的第5行出了点问题,对吧?以下是:

代码语言:javascript
复制
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:

代码语言:javascript
复制
    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",
            },
        });
    };
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-05 19:54:05

当您从超类继承时,错误看起来来自于extends函数TypeScript注入。

看看代码,我会说当您使用它时,您的DataListener是不可用的:

代码语言:javascript
复制
extends DataListener 

它可能不是完全丢失的,否则编译器会警告您--所以在Jasmine运行时它不是包含(即加载),就是没有顺序加载。

把他们整理好希望..。joy!

代码语言:javascript
复制
        files: [
            'Scripts/DeviceData/DataListener.ts',
            'Scripts/DeviceData/ConsoleListener.ts',
            'Scripts/UnitTests/*.spec.ts' 
        ],
票数 3
EN

Stack Overflow用户

发布于 2017-10-05 19:56:11

function由TypeScript编译器生成,以处理类继承。b表示基类,d表示派生类。因此,问题是缺少基类DataListener。检查编译和打包脚本的方式。一个名称空间(参见module关键字)可以跨多个文件定义,但它们的收集/绑定必须通过手工或使用--outFile来处理。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46593647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档