简体   繁体   English

HttpTestingController ExpectOne仅验证api端点

[英]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.

相关问题 带有查询参数的 HttpTestingController.expectOne() - HttpTestingController.expectOne() with queryparameters HttpTestingController expectOne 匹配 URL 错误 - HttpTestingController expectOne Match URL error 如何将任何 URL 与 HttpTestingController.expectOne 匹配? - How to match any URL with HttpTestingController.expectOne? 当实际请求嵌套在承诺中时,HttpTestingController expectOne 不起作用 - HttpTestingController expectOne not working when actual request is nested within a promise 带笑话的角度:`verify()vs.expectOne()`? - Angular w/Jest: `verify()` vs.`expectOne()`? 如何使用Angular的HttpTestingController检查是否已向同一API端点发出2个HTTP请求? - How can I check that 2 HTTP requests have been made to the same API endpoint using Angular's HttpTestingController? 即使调用了 httpClient.get,Angular httpTestingController.expectOne 也会抛出 - Angular httpTestingController.expectOne throws even though httpClient.get was called Angular 4.3中的单元测试HttpClientModule:来自HttpTestingController.expectOne(url)的未定义返回值 - Unit testing HttpClientModule in Angular 4.3: undefined return value from HttpTestingController.expectOne(url) HttpTestingController.expectOne 看不到我的请求,但是在 afterEach() 方法中是打开的 - HttpTestingController.expectOne can't see my request, but it is open in afterEach() method 为什么我已经有了 expectOne() 还需要 verify()? - Why do I need verify() when I already have expectOne()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM