gpt4 book ai didi

angular - HttpTestingController ExpectOne匹配URL错误

转载 作者:行者123 更新时间:2023-12-02 02:52:36 24 4
gpt4 key购买 nike

我正在测试我的http服务类并尝试测试请求,但是我这个错误

Expected one matching request for criteria "Match URL: https://beneficios/api/v1/processobeneficio/1111111111111/timeline", found none.

我不知道为什么,我在服务和测试类上使用相同的参数。

服务等级
export class AcompanhamentoStatusService {

constructor(private http: HttpClient) { }

public ObterStatusResgate<T>(protocolo: string): Observable<T> {

return this.http.get<T>(environment.apiEndpoint + 'processobeneficio/' + protocolo + '/timeline' );
}

测试
describe('AcompanhamentoStatusService', () => {
let service: AcompanhamentoStatusService;
let httpMock: HttpTestingController;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [ AcompanhamentoStatusService ],
imports: [ HttpClientTestingModule ],
});
service = TestBed.get(AcompanhamentoStatusService);
httpMock = TestBed.get(HttpTestingController);

});

it('should have apiEndpoint in Enviroment', () => {
expect(environment.apiEndpoint).toBeDefined();
});

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

it('Deve retornar status acompanhamento', () => {
const protocolo = '1111111111111';
const mockPassos = [
{ status: 'Em Análise', data: '20/08/2018'},
{ status: 'Pago', data: '25/08/2018'},
];

service.ObterStatusResgate(protocolo).subscribe( passos => {
expect(passos).toBe(mockPassos);
});

const req = httpMock.expectOne(environment.apiEndpoint + 'processobeneficio/' + protocolo + '/timeline' );
expect(req.request.method).toBe('GET');
req.flush(mockPassos);
});

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

所有测试都通过了,只有一个失败了,不知道为什么会这样吗?

最佳答案

我遇到了同样的问题,我在项目中用注释了类似于您的代码的三行代码,并得到了我的代码使用的真实URL,因此:

const req = httpMock.expectOne(environment.apiEndpoint + 'processobeneficio/' + protocolo + '/timeline' );
expect(req.request.method).toBe('GET');
req.flush(mockPassos);

然后,我得到了当前的 URL ,我知道这是不匹配的。

关于angular - HttpTestingController ExpectOne匹配URL错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51408914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com