Can't capture HTTP request with robolectric

喜欢而已 提交于 2019-12-06 13:47:19

问题


I am trying to capture an http request with Robolectric The method tokenize method sends a request to a server and I just want to test that the post is sent: If I try

//Cardtest.java

Robolectric.setDefaultHttpResponse(200, "dummy");
card.tokenize(paymentHandler);
Robolectric.getSentHttpRequest(0);

The I have an empty array error

But I know that the request is sent because if I remove the first line I have the following error:

Unexpected HTTP call POST

If I put log statement it appears that my success block is never called for an http request.

How can I make sure that my HTTP request success call back get called.

(I already try Robolectric.runUiThreadTasksIncludingDelayedTasks();)

Thanks


回答1:


As we discussed in comments. The issue was because Robolectric was configured to do not intercept http requests:

Robolectric.getFakeHttpLayer().interceptHttpRequests(false)



回答2:


Above the 3.0 and newer version You can use

 FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);


来源:https://stackoverflow.com/questions/20708844/cant-capture-http-request-with-robolectric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!