在 Angular 测试中,对使用 HttpModule 服务的组件的概念感到困惑
In Angular test, confused about the concept on a component that uses a service that uses HttpModule
我对 Angular 很陌生。
我在了解框架的运行方式后正在看测试
我发现一个使用服务的组件,如果该服务使用 HttpModule,组件测试规范文件也需要导入 HttpModule。不太明白背后的概念。
我认为使用模块的组件不需要知道服务是如何工作的,因为它是服务执行的一种封装过程。更改不更改 api 的服务也不应破坏组件。
如果我的理解是正确的,那么如果有一个组件在开发之初没有使用HttpModule,那么有人基于该服务编写一个组件,然后由开发人员双方编写测试。有一天,服务开发人员决定使用HttpModule,但组件开发人员可能不知道,在这种情况下组件测试会失败。
为什么我们需要将 HttpModule 放入组件规范文件中?
编辑:
例子如下
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { UserComponent } from './user.component';
import { DataService } from '../../services/data.service';
describe('UserComponent', () => {
let component: UserComponent;
let fixture: ComponentFixture<UserComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ UserComponent ],
imports: [
FormsModule,
HttpModule, // this line is need to pass the test
],
providers: [
DataService, // because this service is using HttpModule
],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(UserComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should be created', () => {
expect(component).toBeTruthy();
});
});
您可以在测试中提供不同的 DataService
,例如:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ UserComponent ],
imports: [
FormsModule,
],
providers: [
{ provide: DataService, useValue: jasmine.createSpyObj('data', ['someMethod']) }
],
})
.compileComponents();
}));
然后您可以测试组件是否与服务正确交互。
component.onClick();
expect(component.dataService.someMethod).toHaveBeenCalled();
我对 Angular 很陌生。
我在了解框架的运行方式后正在看测试
我发现一个使用服务的组件,如果该服务使用 HttpModule,组件测试规范文件也需要导入 HttpModule。不太明白背后的概念。
我认为使用模块的组件不需要知道服务是如何工作的,因为它是服务执行的一种封装过程。更改不更改 api 的服务也不应破坏组件。
如果我的理解是正确的,那么如果有一个组件在开发之初没有使用HttpModule,那么有人基于该服务编写一个组件,然后由开发人员双方编写测试。有一天,服务开发人员决定使用HttpModule,但组件开发人员可能不知道,在这种情况下组件测试会失败。
为什么我们需要将 HttpModule 放入组件规范文件中?
编辑: 例子如下
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { UserComponent } from './user.component';
import { DataService } from '../../services/data.service';
describe('UserComponent', () => {
let component: UserComponent;
let fixture: ComponentFixture<UserComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ UserComponent ],
imports: [
FormsModule,
HttpModule, // this line is need to pass the test
],
providers: [
DataService, // because this service is using HttpModule
],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(UserComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should be created', () => {
expect(component).toBeTruthy();
});
});
您可以在测试中提供不同的 DataService
,例如:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ UserComponent ],
imports: [
FormsModule,
],
providers: [
{ provide: DataService, useValue: jasmine.createSpyObj('data', ['someMethod']) }
],
})
.compileComponents();
}));
然后您可以测试组件是否与服务正确交互。
component.onClick();
expect(component.dataService.someMethod).toHaveBeenCalled();