问题
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