typescript 错误:StaticInjectorError(DynamicTestModule)[CityService -> Http]:没有 Http 提供者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50462435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:25:42  来源:igfitidea点击:

Error: StaticInjectorError(DynamicTestModule)[CityService -> Http]: No provider for Http

angulartypescripttestingjasmine

提问by jacekbj

I have this service that return all City from ws.

我有这项服务,可以从 ws 返回所有城市。

@Injectable()
export class CityService {
  constructor(private http: Http, private router: Router,
  private auth: AuthService) { }

  public getAllCity(): Observable<City[]> {
    let headers = new Headers();
    headers.append('x-access-token', this.auth.getCurrentUser().token);
    return this.http.get(Api.getUrl(Api.URLS.getAllCity), {
      headers: headers
    })
      .map((response: Response) => {
        let res = response.json();
        if (res.StatusCode === 1) {
          this.auth.logout();
        } else {
          return res.StatusDescription.map(city => {
            return new City(city);
          });
        }
      });
  }
}

Now, I tried this code to test my service. In this post I want to know, How to test this service CityService

现在,我尝试使用此代码来测试我的服务。在这篇文章中,我想知道如何测试此服务CityService

describe('Service: City', () => {
    let component: CityService;
    let fixture: ComponentFixture<CityService>;

    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [],
            providers: [CityService] 
        })
        fixture = TestBed.get(CityService);
        component = fixture.componentInstance;
    });
   it('#getAllCity should return real value', () => {
     expect(component.getAllCity()).toBe('real value');
  });
});

I tried this code, but show me error:

我试过这段代码,但显示错误:

Error: StaticInjectorError(DynamicTestModule)[CityService -> Http]:
StaticInjectorError(Platform: core)[CityService -> Http]: NullInjectorError: No provider for Http!

错误:StaticInjectorError(DynamicTestModule)[CityService -> Http]:
StaticInjectorError(Platform: core)[CityService -> Http]: NullInjectorError: No provider for Http!

How to test / how to show my city in ng test?

如何测试/如何显示我所在的城市ng test

This is my first attempt, can you suggest me any example, or tutorial like my code?

这是我的第一次尝试,你能给我推荐任何例子,或者像我的代码一样的教程吗?

回答by jacekbj

CityServicedepends on 3 services, namely Http, Routerand AuthService. You need to inject them into the test.

CityService取决于 3 个服务,即Http,RouterAuthService。您需要将它们注入测试中。

describe('Service: City', () => {
    let service: CityService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [],
            providers: [CityService, AuthService],  // all all services upon which AuthService depends
            imports: [RouterTestingModule, HttpClientTestingModule], 
        });
    });

    beforeEach(() => {
        service = TestBed.get(CityService);
    });

    it('#getAllCity should return real value', () => {
        expect(service.getAllCity()).toBe('real value');
    });
});

https://angular.io/guide/testing- this is a must-read for unit testing Angular. https://angular.io/guide/testing#service-tests- part related to testing services. https://angular.io/guide/http#setup-1- related to testing calls made with Http service.

https://angular.io/guide/testing- 这是单元测试 Angular 的必读之物。 https://angular.io/guide/testing#service-tests- 与测试服务相关的部分。 https://angular.io/guide/http#setup-1- 与使用 Http 服务进行的测试调用相关。