首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >气象API测试

气象API测试
EN

Stack Overflow用户
提问于 2022-02-23 22:16:06
回答 1查看 78关注 0票数 0

我有一项根据天气数据提供数据的服务:

代码语言:javascript
复制
getCurrentWeatherData(location: Location, target?: string) {
    return this.http.get<CurrentWeatherAPI>(
        `${this.apiUrl}current?${
            target === 'inputBtn' || target === 'enter'
                ? '&city=' + location.city
                : '&lat=' + location.latitude + '&lon=' + location.longitude
        }&key=${this.apiKey}&lang=pl`
    );
}

并对此进行测试:

代码语言:javascript
复制
describe('GetWeatherDataService', () => {
    let httpController: HttpTestingController;
    let service: GetWeatherData;
    const location = {
        city: 'name',
        latitude: 67,
        longitude: 10,
    };

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            imports: [HttpClientTestingModule],
            providers: [GetWeatherData, HttpClient],
        }).compileComponents();

        service = TestBed.inject(GetWeatherData);
        httpController = TestBed.inject(HttpTestingController);
    });

    it('service should be created', () => {
        expect(service).toBeTruthy();
    });

    it('#getCurrentWeatherData should use GET to retrieve data', () => {
        service.getCurrentWeatherData(location, 'enter').subscribe();

        const testRequest = httpController.expectOne(
            'https://api.weatherbit.io/v2.0/current?&city=Name&key=1929292&lang=eu'
        );
        expect(testRequest.request.method).toEqual('GET');
    });
});

我的问题是,我想检查来自变量位置的城市名称是否等于从API获得的实际数据。我不知道如何使用测试模块从API中获取json。

EN

回答 1

Stack Overflow用户

发布于 2022-02-23 23:20:05

您不应该在unitTests中使用真正的API。而不是这个模拟的HttpClient。

https://angular.io/guide/testing-services#testing-http-services

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71244681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档