使用浏览器phantomJS进行单元测试业力并不支持导入继承

时间:2017-03-30 02:55:53

标签: inheritance phantomjs karma-jasmine

我在用phantomjs进行单元测试时遇到了这个问题。我认为浏览器phantomjs不支持导入继承。以下是问题详情:

环境:

  • karma 1.5.0
  • phantomjs:2.1.1(使用浏览器)

我有这些模型类:

base.model.ts

export class BaseModel {
    id?: number;
    name?: string;
}

customer.model.ts

import { BaseModel  } from './base.model';

export class CustomerModel extends  BaseModel {
    address?: string;
    // more properties defined
}

home.model.ts

import { BaseModel  } from './base.model';
import { CustomerModel } from './customer.model';

export class HomeModel extends  BaseModel {
    param1: CustomerModel;

    constructor(data?: any) { // write some logic in here }
}

home.service.ts

import { Injectable } from '@angular/core';
import { Home } from './home.model';

@Injectable()
export class HomeService {
    constructor(){  }

    get() {
        let home = new HomeModel();
    }
}

重现行为的步骤

  1. 为HomeService的函数get()编写单元测试 (home.service.ts)
  2. 运行单元测试:npm run test将执行karma start config/karma.conf.js
  3. 错误

      

    PhantomJS 2.1.1(Windows 8 0.0.0)   ERROR TypeError:undefined:不能设置为原型!

    根本原因: 经过几天的调查,我发现当单元测试通过new HomeModel()时,问题才会发生。然后它与BaseModelextends相关,我想当我们new HomeModel()时,它首先初始化BaseModel(导入将从上到下),然后{之后初始化{1}}。在CustomerModel中,它仍然导入CustomerModelBaseModel

1 个答案:

答案 0 :(得分:0)

面临与"typescript": "2.4.0"相同的问题:

更新了以下依赖项,并且有所帮助:

"@types/core-js": "0.9.42",
"html-loader": "^0.5.1",
"jasmine": "^2.7.0",
"jasmine-core": "^2.7.0",
"karma": "^1.7.0",
"karma-webpack": "^2.0.4",