我已经检查了这个问题的github问题,并查看了各种堆栈溢出问题和博客和内容,我无法找到解决为什么在运行" ng test&#时出现此错误的原因34;在我的角度2应用程序。
以下是测试的结果:
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 test 05 04 2017 18:19:47.574:WARN [业力]:没有捕获的浏览器, open 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 [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应该创建 应用失败 失败:无法阅读财产'替换'未定义的 TypeError:无法读取属性'替换'未定义的 at _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) 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)应用程序:MemberAdmin应具有as 标题' app有效!'失败 失败:无法阅读财产'替换'未定义的 TypeError:无法读取属性'替换'未定义的 at _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) 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 秒/ 0.469秒)Chrome 56.0.2924(Mac OS X 10.10.5)错误
Disconnectedundefined Chrome 56.0.2924(Mac OS X 10.10.5):已执行3 3(2次失败)(0.638秒/ 0.469秒)
答案 0 :(得分:4)
我也碰到了这个。 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,
...
```
如果您不需要测试路由并仅测试组件,则不应在这些测试中提供Location
和LocationStrategy
个提供程序。