Error: Expected one matching request for criteria ... found none

Error: Expected one matching request for criteria ... found none

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

我正在尝试测试此服务,它是一个 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();            

    });
    
});

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

Error: Expected one matching request for criteria "Match URL: <<the_expected_url>>, found none.

如果我更改测试以便注释掉对 expectOne 和 flush 的调用并调用验证,那么测试将变为:

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

    });

我现在看到的错误是:

Error: Expected no open requests, found 1: GET <<the_expected_url>>

我做错了什么?

我不认为你做错了什么。我遇到了和你一样的问题,我无法处理请求,这很可能是因为请求有参数,我们无法通过参数匹配它,尽管 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');
                    
    });