Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fourlastor/rx-dispatcher
An asynchronous request dispatcher for testing
https://github.com/fourlastor/rx-dispatcher
Last synced: 6 days ago
JSON representation
An asynchronous request dispatcher for testing
- Host: GitHub
- URL: https://github.com/fourlastor/rx-dispatcher
- Owner: fourlastor
- License: mit
- Created: 2015-07-22T15:28:47.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-08-12T09:00:12.000Z (over 9 years ago)
- Last Synced: 2024-12-06T20:04:12.523Z (21 days ago)
- Language: Java
- Size: 199 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rx-dispatcher (WIP)
An asynchronous request dispatcher for testing http calls.This is a request handler for [RxNetty](https://github.com/ReactiveX/RxNetty/) that will hang requests until you set a response.
```java
RxDispatcher rxDispatcher = new RxDispatcher();
HttpServer server = RxNetty.createHttpServer(8080, rxDispatcher);
server.start();// do request to http://localhost:8080/hello - it will hang
rxDispatcher.match("/hello", "world");
// the server will respond to the request previously made with "hello"
```## Installation
Add the correct repository to your gradle file
```
repositories {
jcenter()
}dependencies {
androidTestCompile 'com.fourlastor:rx-dispatcher:0.0.1'
}
```## Motivation
Writing Android instrumentation tests was quite difficoult with [MockWebServer](https://github.com/square/okhttp/tree/master/mockwebserver) because the activity would start (and do the requests) **before** the test even started. That's especially true if you use the new `@Rule` system.
One workaround is to extend the old `ActivityInstrumentationTestCase2` and delay `getActivity()` after you setup your calls with `MockWebServer`. I found this approach complicated and not clean.
Inspired by [RxPresso](https://github.com/novoda/rxpresso/) I wrote this. I understood the motivations behind RxPresso but don't agree in mocking parts of the application in an end to end test.
Writing a test now will be as simple as this:
```java
@RunWith(AndroidJUnit4.class)
public class MyCoolActivityTest {
private static int PORT = 8080;
private RxDispatcher rxDispatcher;
private HttpServer server;
private RxDispatcher rxDispatcher;
@Rule
public ActivityTestRule rule = new ActivityTestRule(MyCoolActivity.class) {
@Override
protected void beforeActivityLaunched() {
rxDispatcher = new RxDispatcher();
server = RxNetty.createHttpServer(PORT, rxDispatcher);
server.start();
// change your app settings to use http://localhost:8080 as the endpoint
}@Override
protected void afterActivityFinished() {
server.shutdown();
}
};@Test
public void helloWorld() throws Exception {
rxDispatcher.match("/hello", "world");
onView(withId(R.id.target_for_hello))
.check(matches(withText("world")));
}
}
```