简体   繁体   English

在Angular测试中,对使用使用HttpModule的服务的组件的概念感到困惑

[英]In Angular test, confused about the concept on a component that uses a service that uses HttpModule

I am pretty new to Angular. 我对Angular很新。

I am looking at the test after understanding how the framework operates. 在了解框架如何运作之后,我正在查看测试。

I found that a component that uses a service and if that service uses HttpModule, the component test spec file also need to have the HttpModule imported. 我发现一个使用服务的组件,如果该服务使用HttpModule,组件测试规范文件也需要导入HttpModule。 I don't really understand the concept behind. 我真的不明白背后的概念。

I think a component that uses the module should not need to know how the service works, as it is kind of an encapsulated process that the service performs. 我认为使用该模块的组件不需要知道服务是如何工作的,因为它是服务执行的一种封装过程。 And changing a service that doesn't change the api should not break the component as well. 更改不改变api的服务也不应该破坏组件。

If my understanding is correct, then if there is a component that don't use HttpModule at the very beginning of the development, then someone write a component base on that service, and both of the developer write tests. 如果我的理解是正确的,那么如果有一个组件在开发的最初阶段没有使用HttpModule,那么有人会根据该服务编写一个组件,并且两个开发人员都会编写测试。 One day, the service developer decided to use the HttpModule but the component developer might not know, the test for the component will fail in this case. 有一天,服务开发人员决定使用HttpModule,但组件开发人员可能不知道,在这种情况下,组件的测试将失败。

Why do we need to get the HttpModule into the component spec file? 为什么我们需要将HttpModule放入组件规范文件中?

Edit: example as follow 编辑:示例如下

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();
  });
});

You can provide different DataService in your test, something like this: 您可以在测试中提供不同的DataService ,如下所示:

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ UserComponent ],
      imports: [
        FormsModule,
      ],
      providers: [
        { provide: DataService,  useValue: jasmine.createSpyObj('data', ['someMethod']) }
      ],
    })
    .compileComponents();
  }));

and then you can test weather the component is correctly interacting with the service. 然后您可以测试组件正确与服务交互的天气。

component.onClick();
expect(component.dataService.someMethod).toHaveBeenCalled();

See more examples here , and in official docs . 在此处以及官方文档中查看更多示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM