【问题标题】:Error: Expected one matching request for criteria ... found none错误:预期有一个匹配条件的请求...没有找到
【发布时间】:2021-02-20 21:17:25
【问题描述】:

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

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

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: >,没有找到。

如果我更改测试以便注释掉对 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 >

我做错了什么?

【问题讨论】:

    标签: angular unit-testing http jasmine


    【解决方案1】:

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

    【讨论】:

    • 非常感谢。如果没有您的帮助,我将无法解决它。
    猜你喜欢
    • 2018-12-07
    • 2019-01-08
    • 1970-01-01
    • 2018-11-18
    • 2023-03-22
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多