首页 > 解决方案 > 错误:预期有一个匹配条件的请求...没有找到

问题描述

我已经看到了很多关于这个错误的问题,但是今天大部分时间都在尝试每个解决方案,我需要你的帮助。

我正在尝试测试这个服务,它是一个 http get 请求

getTrainInformation(url: string): Observable<any> {
        return this.http.get(url,
            {
                params:
                {
                    expand: 'true'
                }
            }
        ).pipe(catchError(this.handleError));
    }

这是我的测试:

import { HttpTestingController, HttpClientTestingModule } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { TrainInformationService } from "./traininformation.service";

describe('traininformation service', () => {
    let service: TrainInformationService;
    let httpTestingController: HttpTestingController;

    beforeEach( () => {
        TestBed.configureTestingModule({
            imports: [HttpClientTestingModule],
            providers: [TrainInformationService]
        });        

        service = TestBed.get(TrainInformationService);
        httpTestingController = TestBed.get(HttpTestingController);
    });
        
    it('should perform a GET', () => {
        
        const url = service.getFullUrl('BOP');

        const urlParam = { param: 'expand', value: 'true'};
        
        const urlWithParams = `${url}?${urlParam.param}=${urlParam.value}`;
        
        service.getTrainInformation(urlWithParams).subscribe();                 
        
        const req = httpTestingController.expectOne(urlWithParams);
        console.log('req is ' + req); 
        
        req.flush('Some');
        
        // httpTestingController.verify();            

    });
    
});

当测试运行时,它失败了:

错误:应为条件“匹配 URL:<<the_expected_url>>,找到一个匹配请求。

如果我更改测试以便注释掉对 expectOne 的调用并刷新并调用 verify,那么测试变为:

it('should perform a GET', () => {
        
        const url = service.getFullUrl('BOP');

        const urlParam = { param: 'expand', value: 'true'};
        
        const urlWithParams = `${url}?${urlParam.param}=${urlParam.value}`;
        
        service.getTrainInformation(urlWithParams).subscribe();                 
        
        // const req = httpTestingController.expectOne(urlWithParams);
        // console.log('req is ' + req); 
        
        // req.flush('Some');
        
        httpTestingController.verify();            

    });

我现在看到的错误是:

错误:预期没有打开的请求,发现 1:GET <<the_expected_url>>

我究竟做错了什么?

标签: angularunit-testinghttpjasmine

解决方案


我不认为你做错了什么。我遇到了和你一样的问题,我无法处理请求,这很可能是因为请求有参数,尽管 URL 似乎是一对一的,但我们无法通过参数匹配它。如果没有参数,我可以轻松处理请求。

为了使用参数处理请求,我使用了不同的签名来httpTestingController.expectOne传递具有匹配条件的对象。

    afterEach(() => {
      httpTestingController.verify();
    });

    it('should perform a GET', () => {
        
        const url = service.getFullUrl('BOP');

        const urlParam = { param: 'expand', value: 'true'};
        
        const urlWithParams = `${url}?${urlParam.param}=${urlParam.value}`;
        
        service.getTrainInformation(urlWithParams).subscribe(response => {
          expect(response).toBe('Some'); // put an assertion here (optional).
        });                 
        
        const req = httpTestingController.expectOne({
           method: 'GET',  // find open Http request that is a get request.
        });
        console.log('req is ' + req); // should have a handle on it now
        // below asserts the params
        expect(req.request.params.toString()).toBe('${urlParam.param}=${urlParam.value}');
        req.flush('Some');
                    
    });

推荐阅读