Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing &引用;错误:没有路由器的提供程序“;在编写Karma Jasmine单元测试用例时_Unit Testing_Angular_Karma Jasmine_Angular2 Routing_Angular Cli - Fatal编程技术网

Unit testing &引用;错误:没有路由器的提供程序“;在编写Karma Jasmine单元测试用例时

Unit testing &引用;错误:没有路由器的提供程序“;在编写Karma Jasmine单元测试用例时,unit-testing,angular,karma-jasmine,angular2-routing,angular-cli,Unit Testing,Angular,Karma Jasmine,Angular2 Routing,Angular Cli,我们已经完成了一个angular2项目设置,并在其中创建了一个模块(我的模块),在该模块内部使用以下命令创建了一个组件(我的新组件): ng new angular2test cd angular2test ng g module my-module ng generate component my-new-component 在创建设置和所有组件之后,我们从angular2test文件夹中的cmd运行ng test命令 下面的文件是我们的my-new-component.component.

我们已经完成了一个angular2项目设置,并在其中创建了一个模块(我的模块),在该模块内部使用以下命令创建了一个组件(我的新组件):

ng new angular2test
cd angular2test
ng g module my-module
ng generate component my-new-component
在创建设置和所有组件之后,我们从angular2test文件夹中的cmd运行
ng test
命令

下面的文件是我们的my-new-component.component.ts文件

从'@angular/core'导入{Component,OnInit};
从'@angular/Router'导入{Router,Routes,RouterModule};
从“../services/dummy.service”导入{DummyService};
@组成部分({
选择器:“应用程序我的新组件”,
templateUrl:“./my new component.component.html”,
样式URL:['./我的新组件.component.css']
})
导出类MyNewComponentComponent实现OnInit{
构造函数(专用路由器:路由器,专用dummyService:dummyService){}
恩戈尼尼特(){
}
重定向():无效{
//this.router.navigate(['/my module/my-new-component-1']))
}
}
以下文件是我们的my-new-component.component.spec.ts文件

/*tslint:disable:没有未使用的变量*/
从“@angular/core/testing”导入{async,ComponentFixture,TestBed};
从“@angular/platform browser”导入{By}”;
从“@angular/core”导入{DebugElement};
从“@angular/router/testing”导入{RouterTestingModule};
从'@ng bootstrap/ng bootstrap'导入{NgbModule};
从“../services/dummy.service”导入{DummyService};
从“./my new component.component”导入{MyNewComponentComponent};
描述('MyNewComponentComponent',()=>{
let组件:MyNewComponentComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
导入:[RouterTestingModule,NgbModule.forRoot(),DummyService],
声明:[MyNewComponentComponent]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(MyNewcomComponentComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
}); 
});
我们在运行ng test命令时遇到以下cmd错误:

    Chrome 54.0.2840 (Windows 7 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.593 secs / 2.007 secs)
Chrome 54.0.2840 (Windows 7 0.0.0) MyNewComponentComponent should create FAILED
        Failed: Unexpected value 'DummyService' imported by the module 'DynamicTestModule'
        Error: Unexpected value 'DummyService' imported by the module 'DynamicTestModule'
我们已经更新了组件文件和等级库文件。很高兴看到下面的代码片段

下面的文件是我们的my-new-component.component.ts文件

从'@angular/core'导入{Component,OnInit};
从'@angular/Router'导入{Router,Routes,RouterModule};
从“../services/dummy.service”导入{DummyService};
@组成部分({
选择器:“应用程序我的新组件”,
templateUrl:“./my new component.component.html”,
样式URL:['./我的新组件.component.css']
})
导出类MyNewComponentComponent实现OnInit{
构造函数(私有路由器:路由器,私有dummyService:dummyService,公共fb:FormBuilder){
超级(fb),;
}
恩戈尼尼特(){
}
重定向():无效{
//this.router.navigate(['/my module/my-new-component-1']))
}
}
以下文件是我们的my-new-component.component.spec.ts文件

/*tslint:disable:没有未使用的变量*/
从“@angular/core/testing”导入{async,ComponentFixture,TestBed};
从“@angular/platform browser”导入{By}”;
从“@angular/core”导入{DebugElement};
从'@angular/forms'导入{FormsModule,FormGroup,FormBuilder,Validators,ReactiveFormsModule};
从“../../common/pipes/string split.pipe”导入{SplitPipe};
从“@angular/router/testing”导入{RouterTestingModule};
从“../services/dummy.service”导入{DummyService};
从“./my new component.component”导入{MyNewComponentComponent};
描述('MyNewComponentComponent',()=>{
let组件:MyNewComponentComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
导入:[RouterTestingModule,DummyService,HttpModule,FormBuilder],
声明:[MyNewComponentComponent,SplitPipe]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(MyNewcomComponentComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
}); 
});
但是在运行ng test命令时,我们得到了以下错误

09 12 2016 09:13:48.987:WARN [karma]: No captured browser, open http://localhost:9876/
09 12 2016 09:13:49.008:INFO [karma]: Karma v1.2.0 server started at http://localhost:9876/
09 12 2016 09:13:49.010:INFO [launcher]: Launching browser Chrome with unlimited concurrency
09 12 2016 09:13:49.420:INFO [launcher]: Starting browser Chrome
09 12 2016 09:13:58.642:INFO [Chrome 54.0.2840 (Windows 7 0.0.0)]: Connected on socket /#QZ9LSSUVeK6KwNDlAAAA with id 46830907
        Failed: Unexpected value 'FormBuilder' imported by the module 'DynamicTestModule'
        Error: Unexpected value 'FormBuilder' imported by the module 'DynamicTestModule'
设置测试模块时需要导入

/*tslint:disable:没有未使用的变量*/
从“@angular/core/testing”导入{async,ComponentFixture,TestBed};
从“@angular/platform browser”导入{By}”;
从“@angular/core”导入{DebugElement};
从“@angular/router/testing”导入{RouterTestingModule};
从“./my new component.component”导入{MyNewComponentComponent};
描述('MyNewComponentComponent',()=>{
let组件:MyNewComponentComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
导入:[RouterTestingModule],
声明:[MyNewComponentComponent]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(MyNewcomComponentComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});
编辑:使用模拟DummyService的示例

/*tslint:disable:没有未使用的变量*/
从“@angular/core/testing”导入{async,ComponentFixture,TestBed};
从“@angular/platform browser”导入{By}”;
从“@angular/core”导入{DebugElement};
从“@angular/router/testing”导入{RouterTestingModule};
从“./my new component.component”导入{MyNewComponentComponent};
//导入服务
从“../dummy.service”导入{DummyService};
//嘲笑服务
班
import {RouterTestingModule} from '@angular/router/testing';

beforeEach(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule], // <====
providers: [],
declarations: [],
});
});
error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'RouterModule', 'Router', 'Function', 'Function' ] })
NullInjectorError: R3InjectorError(DynamicTestModule)[RouterModule -> Router -> Function -> Function]: 
  NullInjectorError: No provider for Function!