【问题标题】:Error: StaticInjectorError(DynamicTestModule)[CityService -> Http]: No provider for Http错误:StaticInjectorError(DynamicTestModule)[CityService -> Http]: No provider for Http
【发布时间】:2018-10-31 21:53:46
【问题描述】:

我有这项服务,可以从 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);
          });
        }
      });
  }
}

现在,我尝试了这段代码来测试我的服务。在这篇文章中我想知道,如何测试这个服务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');
  });
});

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

错误:StaticInjectorError(DynamicTestModule)[CityService -> Http]:
StaticInjectorError(平台:核心)[CityService -> Http]: NullInjectorError: 没有 Http 提供者!

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

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

【问题讨论】:

  • 您解决了这个问题吗?
  • 当然是@gentlyawesome

标签: angular typescript testing jasmine


【解决方案1】:

CityService 依赖于 3 个服务,即HttpRouterAuthService。您需要将它们注入到测试中。

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 - 这是 Angular 单元测试的必读内容。 https://angular.io/guide/testing#service-tests - 与测试服务相关的部分。 https://angular.io/guide/http#setup-1 - 与测试使用 Http 服务进行的调用有关。

【讨论】:

  • 我修改了providers: [CityService,Http, AuthService, ConnectionBackend ], imports: [RouterTestingModule, HttpModule],现在显示这个错误:TypeError: Cannot read property 'token' of null
  • 那是另一个问题,与这个问题无关。
  • 您不应该将真正的 http 客户端注入测试上下文;单元测试发货进行网络调用,我认为它无论如何都不会工作。
  • 感谢@jonrsharpe 的评论,我更新了答案。
  • Error: StaticInjectorError(DynamicTestModule)[CityService -&gt; Http]: StaticInjectorError(Platform: core)[CityService -&gt; Http]: NullInjectorError: No provider for Http!
猜你喜欢
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 2019-01-14
相关资源
最近更新 更多