Hi,
we are currently using both Espresso 2 and Robolectric based tests. Now I was wondering: is there a way to mock a socket to bind on in order to create some test events?
Sorry, I'm not familiar with Robolectric. On the unittest of the library, we just spin up a socket.io server and test against it.
I'm also interested in a way to mock the server without the need of having a local instance of the socket.io server running.
I'm using this library to mock my HTTP server https://github.com/square/okhttp/tree/master/mockwebserver
I would like to be able to do something similar. Like
sServer = new SocketServerMock("/myScope");
sServer.start(); // maybe this will trigger the connect event in the client
// test something and assert an action triggered by an event
sServer.emit(eventName, eventPayload);
// boom, the action got triggered.
sServer.stop(); // this could trigger the disconnect
Looking at the code my first impression on how to do this is to extend the Manager class and provide the functionality to always be connected and a method to emit an event, then in the Socket or Options class set this Manager to be used. Do you think this is a good approach? if so I might start working on something.
Some of the private definitions in the classes did not allowed me to successfully create a stub of the Manager without rewriting a lot of code, but in the meantime I just used the Emitter class as my socket object.
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.Socket;
import org.json.JSONException;
import org.json.JSONObject;
public class SocketServerStub {
protected Emitter socket = new Emitter();
public SocketServerStub() {}
public Emitter getSocket() {
return socket;
}
public void start() {
socket.emit(Socket.EVENT_CONNECT);
}
public void emit(String event, String jsonString) throws JSONException {
JSONObject obj = new JSONObject(jsonString);
socket.emit(event, obj);
}
public void stop() {
socket.emit(Socket.EVENT_DISCONNECT);
}
}
It supports the main functionality I wanted.
The idea of having such an emitter is quite nice. Just to make sure: how can we make sure that the mockwebserver is then the one to bind our sockets to?
I ended up doing something a bit different, I defined these two classes SocketStub.java and SocketServerStub.java.
Then after creating the SocketServerStub object I pass the socket object into my singleton class in charge of managing the socket connection like this
SocketServerStub socketServer = new SocketServerStub();
MySingletonSocketManager.getInstance().setSocket(socketServer.getSocket());
and then I just need to run
socketServer.emit("myEvent", "myPayload");
to make sure an event is emitted.
This is a basic stubbing of the socket client, it's intended just to trigger actions inside the app that will only get triggered with a socket event.
Most helpful comment
I ended up doing something a bit different, I defined these two classes SocketStub.java and SocketServerStub.java.
Then after creating the SocketServerStub object I pass the socket object into my singleton class in charge of managing the socket connection like this
and then I just need to run
to make sure an event is emitted.
This is a basic stubbing of the socket client, it's intended just to trigger actions inside the app that will only get triggered with a socket event.