问题描述
当我尝试执行HTTP单元测试用例时出现此错误
I am getting this error when i try to execute HTTP unit test cases
我正在使用Angular5.如何解决此问题
I am using Angular 5. How can i resolve this
以下是我的常规GET代码.下面的代码只是带来普通的GET
Below is my code for normal GET. Below code just brings normal GET
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController, TestRequest } from
'@angular/common/http/testing';
import { DangerService } from './danger.service';
import { DangerFlag } from '../danger.model';
describe('DataService Tests', () => {
let dataService: DangerService;
let httpTestingController: HttpTestingController;
let testDangerFlags: DangerFlag[] = [ "sample data" ]
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
providers: [ DangerService ]
});
dataService = TestBed.get(DangerService);
httpTestingController = TestBed.get(HttpTestingController);
});
afterEach(() => {
httpTestingController.verify();
});
fit('should get all danger flags', () => {
dataService.getDangerFlagDetails()
.subscribe((data: DangerFlag[]) => {
expect(data.length).toBe(3);
});
});
});
推荐答案
在您发出模拟请求但不完成/关闭"该请求时会发生这种情况.运行测试后,打开的请求可能会保持打开状态,最终会导致内存泄漏,特别是如果测试多次运行.
This happens when you make a mock request, but don't 'complete/close' it. An open request may stay open after a test is run, eventually memory leaking, especially if the test is ran multiple times.
订阅模拟请求就客户端而言调用它,但就后端而言并没有完成"它.可以通过多种方式完成"请求;
Subscribing to a mock request calls it as far as the client side is concerned but does not 'complete' it as far as the backend is concerned. 'Completing' a request can be done in a number of ways;
backend = TestBed.get(HttpTestingController)
-
backend.expectOne(URL)
-这将同时测试网址,并关闭"后端调用.这不会测试参数,并且如果您的查询中包含参数,则测试将失败. -
backend.expectNone(URL)
-如果您要测试具有参数的网址,则expectOne()
将不起作用.您必须使用backend.match()
. Match不会自动关闭后端api调用,因此您可以在expectNone()
之后将其关闭. -
.flush(RESPONSE)
-flush将强制发送http呼叫的响应,然后关闭该呼叫.注意:如果在match()
上调用flush,请注意是否有匹配项返回数组,即backend.match(...)[0].flush({})
backend.expectOne(URL)
- this will both test for a url, and 'close' the backend call. This will not test for params, and will fail if your query has params in it.backend.expectNone(URL)
- in case you're testing for urls that have params,expectOne()
wont work. You'll have to usebackend.match()
. Match does not auto close the backend api call, so you canexpectNone()
after it to close it out..flush(RESPONSE)
- flush will force-send a response for the http call, and subsequently close the call. Note: if calling flush on amatch()
, watch out for match returning an array, i.e.backend.match(...)[0].flush({})
这些方法中的任何一种都会关闭http请求,并使backend.verify()
起作用.
Any of these methods will close out the http request, and make backend.verify()
behave.
- 您可以在这里
-
expectOne()
,expectNone()
和match()
都返回 TestRequest的实例
- You can find in depth examples, and more explanations here
expectOne()
,expectNone()
, andmatch()
all return an instance of TestRequest
这篇关于错误:未找到任何未解决的请求,发现1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!