[英]HttpTestingController expectOne verify only the api endpoint
I was would like to only test if request is beiing send to the correct api endpoint. 我只想测试请求是否被发送到正确的api端点。 For example: I have a servis that sends request to http://localhost:port/sth/sth2 and i want to chcek only if there was http://localhost:port . 例如:我有一个Servis将请求发送到http:// localhost:port / sth / sth2,并且我只想在有http:// localhost:port的情况下才使用chcek。 I'm using HttpTestingController and expectOne function, is it possible to do ? 我正在使用HttpTestingController和ExpectOne函数,可以这样做吗?
Is this the sort of thing you were looking to do? 这是您想要做的事情吗?
Setup 设定
let httpClient: HttpClient;
let httpTestingController: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ]
});
httpClient = TestBed.get(HttpClient);
httpTestingController = TestBed.get(HttpTestingController);
});
Test 测试
it('can test HttpClient.get', () => {
const testData: Data = {name: 'Test Data'};
httpClient.get<Data>('http://localhost:8085/test/suite')
.subscribe(data =>
expect(data).toEqual(testData)
);
const request = httpTestingController.expectOne(
(req: HttpRequest<any>) => req.url.includes('http://localhost:8085'));
request.flush(testData);
httpTestingController.verify();
});
It's the example from Angular's documentation, except you pass a function to expectOne
, that takes a req and returns a boolean that checks if the url of the request contains the right endpoint. 这是Angular文档中的示例,除了您将函数传递给expectOne
,该函数接受一个req并返回一个布尔值,该布尔值检查请求的URL是否包含正确的端点。
Of course, you'll swap out the HttpClient with your service. 当然,您将用服务替换HttpClient。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.