angular 2“失败:无法读取未定义的属性'replace'"进行ng测试时 [英] angular 2 "Failed: Cannot read property 'replace' of undefined" when running ng test

查看:80
本文介绍了angular 2“失败:无法读取未定义的属性'replace'"进行ng测试时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have checked the github issues for this issue and looked around on various stack overflow questions and blogs and stuff, and I can't find a solution to why I get this error when running "ng test" in my angular 2 app.

这是测试的样子:

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看起来像这样:

My package.json looks like this:

{
  "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时的输出:

This is the output when running ng test:

ng test 2017年5月4日18:19:47.574:WARN [karma]:未捕获浏览器, 打开 http://localhost:9876/ 2017年5月4日18:19:47.606:INFO [karma]: Karma v1.2.0服务器从 http://localhost:9876/开始于2017年5月4日 18:19:47.607:INFO [启动器]:无限启动浏览器 并发性05 04 2017 18:19:47.614:INFO [启动器]:启动浏览器 Chrome 05 04 2017 2017 18:19:49.239:INFO [Chrome 56.0.2924(Mac OS X 10.10.5)]:已连接至编号为74276965的套接字/#XvtU7IkkctSlo1kYAAAA上Chrome 56.0.2924(Mac OS X 10.10.5)应用:MemberAdmin应该创建 应用失败 失败:无法读取未定义的属性替换" TypeError:无法读取未定义的属性替换" 在_stripIndexHtml(webpack:///~/@angular/common/src/location/location.js:224:0<- src/test.ts:9952:15) 在新位置(webpack:///~/@angular/common/src/location/location.js:41:0<- src/test.ts:9769:54) 在DynamicTestModuleInjector.get(/DynamicTestModule/module.ngfactory.js:125:61) 在DynamicTestModuleInjector.getInternal(/DynamicTestModule/module.ngfactory.js:187:48) 在DynamicTestModuleInjector.NgModuleInjector.get(webpack:///~/@angular/core/src/linker/ng_module_factory.js:139:25<- src/test.ts:19158:44) 在TestBed.get(webpack:///~/@angular/core/bundles/core-testing.umd.js:826:0<- src/test.ts:6078:51) 在CompiledTemplate.proxyViewClass.AppView.injectorGet(webpack:///~/@angular/core/src/linker/view.js:152:0<- src/test.ts:29850:45) 在CompiledTemplate.proxyViewClass.DebugAppView.injectorGet (webpack:///~/@angular/core/src/linker/view.js:580:0<- src/test.ts:30278:49) 在CompiledTemplate.proxyViewClass.View_AppComponent_Host0.createInternal (/DynamicTestModule/AppComponent/host.ngfactory.js:15:63) 在CompiledTemplate.proxyViewClass.AppView.createHostView(webpack:///~/@angular/core/src/linker/view.js:108:0<- src/test.ts:29806:21) 在CompiledTemplate.proxyViewClass.DebugAppView.createHostView (webpack:///~/@angular/core/src/linker/view.js:564:0<- src/test.ts:30262:52) 在ComponentFactory.create(webpack:///~/@angular/core/src/linker/component_factory.js:202:0<- src/test.ts:13033:25) 在initComponent(webpack:///~/@angular/core/bundles/core-testing.umd.js:865:0<- src/test.ts:6117:53) 在ZoneDelegate.invoke(webpack:///~/zone.js/dist/zone.js:334:0<-src/test.ts:37048:26) 在AsyncTestZoneSpec.onInvoke上(webpack:///~/zone.js/dist/async-test.js:49:0<-src/test.ts:23684:39) Chrome 56.0.2924(Mac OS X 10.10.5)应用:MemberAdmin应该具有 标题应用有效!"失败的 失败:无法读取未定义的属性替换" TypeError:无法读取未定义的属性替换" 在_stripIndexHtml(webpack:///~/@angular/common/src/location/location.js:224:0<- src/test.ts:9952:15) 在新位置(webpack:///~/@angular/common/src/location/location.js:41:0<- src/test.ts:9769:54) 在DynamicTestModuleInjector.get(/DynamicTestModule/module.ngfactory.js:125:61) 在DynamicTestModuleInjector.getInternal(/DynamicTestModule/module.ngfactory.js:187:48) 在DynamicTestModuleInjector.NgModuleInjector.get(webpack:///~/@angular/core/src/linker/ng_module_factory.js:139:25<- src/test.ts:19158:44) 在TestBed.get(webpack:///~/@angular/core/bundles/core-testing.umd.js:826:0<- src/test.ts:6078:51) 在CompiledTemplate.proxyViewClass.AppView.injectorGet(webpack:///~/@angular/core/src/linker/view.js:152:0<- src/test.ts:29850:45) 在CompiledTemplate.proxyViewClass.DebugAppView.injectorGet (webpack:///~/@angular/core/src/linker/view.js:580:0<- src/test.ts:30278:49) 在CompiledTemplate.proxyViewClass.View_AppComponent_Host0.createInternal (/DynamicTestModule/AppComponent/host.ngfactory.js:15:63) 在CompiledTemplate.proxyViewClass.AppView.createHostView(webpack:///~/@angular/core/src/linker/view.js:108:0<- src/test.ts:29806:21) 在CompiledTemplate.proxyViewClass.DebugAppView.createHostView (webpack:///~/@angular/core/src/linker/view.js:564:0<- src/test.ts:30262:52) 在ComponentFactory.create(webpack:///~/@angular/core/src/linker/component_factory.js:202:0<- src/test.ts:13033:25) 在initComponent(webpack:///~/@angular/core/bundles/core-testing.umd.js:865:0<- src/test.ts:6117:53) 在ZoneDelegate.invoke(webpack:///~/zone.js/dist/zone.js:334:0<-src/test.ts:37048:26) 在AsyncTestZoneSpec.onInvoke上(webpack:///~/zone.js/dist/async-test.js:49:0<-src/test.ts:23684:39) Chrome 56.0.2924(Mac OS X 10.10.5):执行3之3(2失败)(0.638 秒/0.469秒)Chrome 56.0.2924(Mac OS X 10.10.5)错误
未连接未定义的Chrome 56.0.2924(Mac OS X 10.10.5):已执行3 之3(2失败)(0.638秒/0.469秒)

ng test 05 04 2017 18:19:47.574:WARN [karma]: No captured browser, open http://localhost:9876/ 05 04 2017 18:19:47.606:INFO [karma]: Karma v1.2.0 server started at http://localhost:9876/ 05 04 2017 18:19:47.607:INFO [launcher]: Launching browser Chrome with unlimited concurrency 05 04 2017 18:19:47.614:INFO [launcher]: Starting browser Chrome 05 04 2017 18:19:49.239:INFO [Chrome 56.0.2924 (Mac OS X 10.10.5)]: Connected on socket /#XvtU7IkkctSlo1kYAAAA with id 74276965 Chrome 56.0.2924 (Mac OS X 10.10.5) App: MemberAdmin should create the app FAILED Failed: Cannot read property 'replace' of undefined TypeError: Cannot read property 'replace' of undefined at _stripIndexHtml (webpack:///~/@angular/common/src/location/location.js:224:0 <- src/test.ts:9952:15) at new Location (webpack:///~/@angular/common/src/location/location.js:41:0 <- src/test.ts:9769:54) at DynamicTestModuleInjector.get (/DynamicTestModule/module.ngfactory.js:125:61) at DynamicTestModuleInjector.getInternal (/DynamicTestModule/module.ngfactory.js:187:48) at DynamicTestModuleInjector.NgModuleInjector.get (webpack:///~/@angular/core/src/linker/ng_module_factory.js:139:25 <- src/test.ts:19158:44) at TestBed.get (webpack:///~/@angular/core/bundles/core-testing.umd.js:826:0 <- src/test.ts:6078:51) at CompiledTemplate.proxyViewClass.AppView.injectorGet (webpack:///~/@angular/core/src/linker/view.js:152:0 <- src/test.ts:29850:45) at CompiledTemplate.proxyViewClass.DebugAppView.injectorGet (webpack:///~/@angular/core/src/linker/view.js:580:0 <- src/test.ts:30278:49) at CompiledTemplate.proxyViewClass.View_AppComponent_Host0.createInternal (/DynamicTestModule/AppComponent/host.ngfactory.js:15:63) at CompiledTemplate.proxyViewClass.AppView.createHostView (webpack:///~/@angular/core/src/linker/view.js:108:0 <- src/test.ts:29806:21) at CompiledTemplate.proxyViewClass.DebugAppView.createHostView (webpack:///~/@angular/core/src/linker/view.js:564:0 <- src/test.ts:30262:52) at ComponentFactory.create (webpack:///~/@angular/core/src/linker/component_factory.js:202:0 <- src/test.ts:13033:25) at initComponent (webpack:///~/@angular/core/bundles/core-testing.umd.js:865:0 <- src/test.ts:6117:53) at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:334:0 <- src/test.ts:37048:26) at AsyncTestZoneSpec.onInvoke (webpack:///~/zone.js/dist/async-test.js:49:0 <- src/test.ts:23684:39) Chrome 56.0.2924 (Mac OS X 10.10.5) App: MemberAdmin should have as title 'app works!' FAILED Failed: Cannot read property 'replace' of undefined TypeError: Cannot read property 'replace' of undefined at _stripIndexHtml (webpack:///~/@angular/common/src/location/location.js:224:0 <- src/test.ts:9952:15) at new Location (webpack:///~/@angular/common/src/location/location.js:41:0 <- src/test.ts:9769:54) at DynamicTestModuleInjector.get (/DynamicTestModule/module.ngfactory.js:125:61) at DynamicTestModuleInjector.getInternal (/DynamicTestModule/module.ngfactory.js:187:48) at DynamicTestModuleInjector.NgModuleInjector.get (webpack:///~/@angular/core/src/linker/ng_module_factory.js:139:25 <- src/test.ts:19158:44) at TestBed.get (webpack:///~/@angular/core/bundles/core-testing.umd.js:826:0 <- src/test.ts:6078:51) at CompiledTemplate.proxyViewClass.AppView.injectorGet (webpack:///~/@angular/core/src/linker/view.js:152:0 <- src/test.ts:29850:45) at CompiledTemplate.proxyViewClass.DebugAppView.injectorGet (webpack:///~/@angular/core/src/linker/view.js:580:0 <- src/test.ts:30278:49) at CompiledTemplate.proxyViewClass.View_AppComponent_Host0.createInternal (/DynamicTestModule/AppComponent/host.ngfactory.js:15:63) at CompiledTemplate.proxyViewClass.AppView.createHostView (webpack:///~/@angular/core/src/linker/view.js:108:0 <- src/test.ts:29806:21) at CompiledTemplate.proxyViewClass.DebugAppView.createHostView (webpack:///~/@angular/core/src/linker/view.js:564:0 <- src/test.ts:30262:52) at ComponentFactory.create (webpack:///~/@angular/core/src/linker/component_factory.js:202:0 <- src/test.ts:13033:25) at initComponent (webpack:///~/@angular/core/bundles/core-testing.umd.js:865:0 <- src/test.ts:6117:53) at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:334:0 <- src/test.ts:37048:26) at AsyncTestZoneSpec.onInvoke (webpack:///~/zone.js/dist/async-test.js:49:0 <- src/test.ts:23684:39) Chrome 56.0.2924 (Mac OS X 10.10.5): Executed 3 of 3 (2 FAILED) (0.638 secs / 0.469 secs) Chrome 56.0.2924 (Mac OS X 10.10.5) ERROR
Disconnectedundefined Chrome 56.0.2924 (Mac OS X 10.10.5): Executed 3 of 3 (2 FAILED) (0.638 secs / 0.469 secs)

推荐答案

我也遇到了这个问题.因为我只是测试组件而不是路由,所以RouterTestingModule为我工作.

I ran into this as well. The RouterTestingModule worked for me since I was just testing the component and not routing.

文档内容如下:

 * 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提供程序.

You should not provide the Location and LocationStrategy providers in these tests if you don't need to test routing and are only testing the component.

这篇关于angular 2“失败:无法读取未定义的属性'replace'"进行ng测试时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆