我已经检查了这个问题的github问题,并查看了各种堆栈溢出问题和博客和内容,我找不到解决方案,为什么我在我的角度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测试时的输出:
ng test 05 04 2017 18:19:47.574:WARN [业力]:没有捕获的浏览器,打开http:// localhost:9876/05 04 2017 18:19:47.606:INFO [karma]:Karma v1.2.0服务器启动在http:// localhost:9876/05 04 2017 18:19:47.607:INFO [launcher]:启动浏览器Chrome无限制并发05 04 2017 18:19:47.614:INFO [launcher]:启动浏览器Chrome 05 04 2017 18 :19:49.239:INFO [Chrome 56.0.2924(Mac OS X 10.10.5)]:已连接到socket /#XvtU7IkkctSlo1kYAAAA,ID为74276965 Chrome 56.0.2924(Mac OS X 10.10.5)应用程序:MemberAdmin应创建应用程序FAILED失败:无法读取未定义的TypeError属性'replace':无法在_stripIndexHtml中读取未定义的属性'replace'(webpack:///~/@angular/common/src/location/location.js:224:0 < - src / test.ts:9952:15)在DynamicTestModuleInjector的新位置(webpack:///~/angular/common/src/location/location.js:41:0 < - src / test.ts:9769:54) . 在DynamicTestModuleInjector.getInternal(/ DynamicT)获取(/DynamicTestModule/module.ngfactory.js:125:61) estModule / module.ngfactory.js:187:48)在DynamicTestModuleInjector.NgModuleInjector.get(webpack:///~/@angular/core/src/linker/ng_module_factory.js:139:25 < - src / test.ts: 19158:44)在CompiledTemplate.proxyViewClass的TestBed.get(webpack:///~/angular/core/bundles/core-testing.umd.js:826:0 < - src / test.ts:6078:51) CompiledTemplate.proxyViewClass.DebugAppView.injectorGet(webpack)中的.AppView.injectorGet(webpack:///~/@angular/core/src/linker/view.js:152:0 < - src / test.ts:29850:45) :///~/@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)在ComponentFactory.create的CompiledTemplate.proxyViewClass.DebugAppView.createHostView(webpack:///~/angular/core/src/linker/view.js:564:0 < - src / test.ts:30262:52) (的WebPack:///在initComponent上的〜/ @ local / core / src / link / component_factory.js:202:0 < - src / test.ts:13033:25)(webpack:///~ / @ angular / core / bundle / 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应该具有 Headers 'app works!' FAILED失败:无法读取未定义TypeError的属性'replace':无法在_stripIndexHtml中读取未定义的属性'replace'(webpack:///~/@angular/common/src/location/location.js:224:0 < - src /test.ts:9952:15)在DynamicTestModuleInjector的新位置(webpack:///~/angular/common/src/location/location.js:41:0 < - src / test.ts:9769:54) .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)at 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.proxy.Cop.ConxyViewClass.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)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):执行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):执行3 of 3(2 FAILED)(0.638秒/ 0.469秒)
1 回答
我也碰到了这个 . RouterTestingModule对我有用,因为我只是测试组件而不是路由 .
以下是文档说的内容:
以下是如何添加它: