Angular Jasmine Integration Test with service using real HTTP
Angular Jasmine Integration Test with service using real HTTP
[Angular 版本:2.4.5]
我正在尝试编写一个 Angular 集成组件单元测试,其中包含一个利用内置 Angular HTTP 库的服务。我无法让我的服务 class 实例化:服务未定义或我得到 "Error: Cannot resolve all parameters for 'RequestOptions'(?)".
我知道您通常会模拟后端(已在其他单元测试中成功完成)或使用 Protractor。其他 SO 问题参考 Angular 1 或答案似乎不起作用。起始码:
@Injectable()
export class ProjectService {
projectFeature: IProjectFeature;
constructor(private http: Http) { }
getProjects(): Observable<IProjects> {
return this.http.get("/api/Projects")
.map((response: Response) => response.json() || {})
.catch(this.handleError);
}
}
// Jasmine spec file:
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
Http,
ProjectService, // service that leverages HTTP
ConnectionBackend
//RequestOptions // removed b/c otherwise can't find parameters error happens
],
imports: [
HttpModule
]
});
}));
it('sample test', async(() => {
var projectService = TestBed.get(ProjectService);
var comp = new ProjectComponent(projectService);
comp.ngOnInit(); // internally, calls projectService.getProjects()
expect(comp.projects[0].name).toEqual("Sample Project Name");
}));
HttpModule
模块应该在TestBed
模块配置中提供。之后,Http
执行真正的 XHR 请求,不需要额外的操作,demo:
beforeEach(() => {
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
TestBed.configureTestingModule({
imports: [
HttpModule
]
});
});
it('should do XHR', async(inject([Http], (http) => {
http.get('test.json').subscribe(res => {
expect(res.json()).toBe(1);
});
})));
[Angular 版本:2.4.5]
我正在尝试编写一个 Angular 集成组件单元测试,其中包含一个利用内置 Angular HTTP 库的服务。我无法让我的服务 class 实例化:服务未定义或我得到 "Error: Cannot resolve all parameters for 'RequestOptions'(?)".
我知道您通常会模拟后端(已在其他单元测试中成功完成)或使用 Protractor。其他 SO 问题参考 Angular 1 或答案似乎不起作用。起始码:
@Injectable()
export class ProjectService {
projectFeature: IProjectFeature;
constructor(private http: Http) { }
getProjects(): Observable<IProjects> {
return this.http.get("/api/Projects")
.map((response: Response) => response.json() || {})
.catch(this.handleError);
}
}
// Jasmine spec file:
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
Http,
ProjectService, // service that leverages HTTP
ConnectionBackend
//RequestOptions // removed b/c otherwise can't find parameters error happens
],
imports: [
HttpModule
]
});
}));
it('sample test', async(() => {
var projectService = TestBed.get(ProjectService);
var comp = new ProjectComponent(projectService);
comp.ngOnInit(); // internally, calls projectService.getProjects()
expect(comp.projects[0].name).toEqual("Sample Project Name");
}));
HttpModule
模块应该在TestBed
模块配置中提供。之后,Http
执行真正的 XHR 请求,不需要额外的操作,demo:
beforeEach(() => {
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
TestBed.configureTestingModule({
imports: [
HttpModule
]
});
});
it('should do XHR', async(inject([Http], (http) => {
http.get('test.json').subscribe(res => {
expect(res.json()).toBe(1);
});
})));