【问题标题】:angular 2 "Failed: Cannot read property 'replace' of undefined" when running ng test运行ng测试时角度2“失败:无法读取未定义的属性'替换'”
【发布时间】:2017-08-31 09:35:44
【问题描述】:

我已经检查了这个问题的 github 问题,并查看了各种堆栈溢出问题以及博客和其他内容,但我找不到解决方案来解决为什么在我的 Angular 2 应用程序中运行“ng test”时出现此错误.

这是一个测试的样子:

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { RouterModule } from "@angular/router";
import { Location,LocationStrategy } from "@angular/common";

describe('App: MemberAdmin', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
      imports:[
        RouterModule
      ],
      providers:[
        Location,
        LocationStrategy
      ]
    });
  });

  it('should create the app', async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    let app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

  it(`should have as title 'app works!'`, async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    let app = fixture.debugElement.componentInstance;
    expect(app.loggedIn).toEqual(false);
  }));

});

我的 package.json 看起来像这样:

{
  "name": "member-admin",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "postinstall": "typings install",
    "start": "ng serve",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^2.4.10",
    "@angular/compiler": "^2.3.1",
    "@angular/compiler-cli": "^2.3.1",
    "@angular/core": "^2.4.10",
    "@angular/forms": "^2.3.1",
    "@angular/http": "^2.3.1",
    "@angular/platform-browser": "^2.4.10",
    "@angular/platform-browser-dynamic": "^2.3.1",
    "@angular/router": "~3.1.0",
    "angular-calendar": "^0.4.0",
    "angular2-datatable": "^0.5.2",
    "angular2-google-maps": "^0.16.0",
    "bootstrap-datepicker": "^1.6.4",
    "bootstrap-timepicker": "^0.5.2",
    "core-js": "^2.4.1",
    "mydatepicker": "^1.7.7",
    "ng2-datepicker": "^1.8.2",
    "ng2-datetime": "^1.2.2",
    "ng2-facebook-sdk": "^2.1.1",
    "ng2-file-upload": "^1.1.4-2",
    "rxjs": "5.0.1",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.7.8"
  },
  "devDependencies": {
    "@angular/cli": "^1.0.0",
    "@types/jasmine": "^2.2.30",
    "@types/jquery": "^2.0.34",
    "@types/node": "^6.0.42",
    "awesome-typescript-loader": "2.2.1",
    "codelyzer": "1.0.0-beta.1",
    "jasmine-core": "2.4.1",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "4.0.9",
    "ts-node": "1.2.1",
    "tslint": "3.13.0",
    "typescript": "2.2.1",
    "typings": "1.0.4",
    "webdriver-manager": "10.2.5"
  }
}

这是运行 ng test 时的输出:

ng 测试 05 04 2017 18:19:47.574:WARN [业力]:没有捕获的浏览器, 打开http://localhost:9876/ 05 04 2017 18:19:47.606:INFO [业力]: Karma v1.2.0 服务器开始于http://localhost:9876/ 05 04 2017 18:19:47.607:INFO [启动器]:无限启动浏览器 Chrome 并发 05 04 2017 18:19:47.614:INFO [启动器]:启动浏览器 Chrome 05 04 2017 18:19:49.239:INFO [Chrome 56.0.2924 (Mac OS X 10.10.5)]:连接到套接字 /#XvtU7IkkctSlo1kYAAAA,ID 为 74276965 Chrome 56.0.2924 (Mac OS X 10.10.5) 应用程序:MemberAdmin 应该创建 应用程序失败 失败:无法读取未定义的属性“替换” TypeError:无法读取未定义的属性“替换” 在 _stripIndexHtml (webpack:///~/@angular/common/src/location/location.js:224:0 已断开连接未定义 Chrome 56.0.2924 (Mac OS X 10.10.5):已执行 3 3 个(2 个失败)(0.638 秒 / 0.469 秒)

【问题讨论】:

  • 代码在哪里?我们将如何处理详细的错误描述
  • 我以为是package.son的问题,所以上传了。但我也会上传一些测试。
  • @Dan 你解决了吗?我有完全相同的问题。
  • @titusfx 我不再有这个问题了......我很抱歉,但我不记得我究竟做了什么来解决它......我做过的一件事,我相信为它做出了贡献,我是否已更新为 angular4。
  • @titusfx 你能在轻量级服务器上构建和运行他的项目吗?是不是只有在运行 ng test 时才会发生?

标签: angular unit-testing angular-cli


【解决方案1】:

我也遇到过这个。 RouterTestingModule 对我有用,因为我只是在测试组件而不是路由。

文档是这样说的:

 * This module sets up the router to be used for testing.
 * It provides spy implementations of {@link Location}, {@link LocationStrategy},
 * and {@link NgModuleFactoryLoader}.

这里是如何添加它:

import {RouterTestingModule} from '@angular/router/testing';

describe('MyComponent', () => {
...
  beforeEach(async(() => {
    TestBed.configureTestingModule({
        imports: [
          RouterTestingModule,
          ...

```

如果您不需要测试路由并且只测试组件,则不应在这些测试中提供 LocationLocationStrategy 提供程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-08
    • 2022-01-13
    • 2021-10-08
    • 1970-01-01
    • 2022-06-22
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多