The WebsocketController accepts a WebSocket connection as an argument. To effectively unit test this, we need to mock the WebSocket connection. However, the Gorilla WebSocket library's connection interface is extensive and challenging to mock directly. To simplify testing, we can create a wrapper interface around the Gorilla WebSocket connection. This wrapper will be easier to mock, allowing us to simulate the WebSocket's behavior with custom channels that facilitate reading and writing data.
The wrapper interface, WebsocketConnection, defines the core methods for testing: ReadJSON, WriteJSON, and Close. Using this interface, we can verify that our controller sends and receives the expected data and behaves as intended.
The code outline below shows how this can be implemented:
type WebsocketConnection interface {
ReadJSON(v interface{}) error
WriteJSON(v interface{}) error
Close() error
}
type GorillaWebsocketConnection struct {
conn *websocket.Conn
}
func NewGorillaWebsocketConnection(conn *websocket.Conn) *GorillaWebsocketConnection {
return &GorillaWebsocketConnection{
conn: conn,
}
}
// Ensures that GorillaWebsocketConnection satisfies the WebsocketConnection interface
var _ WebsocketConnection = (*GorillaWebsocketConnection)(nil)
In our test setup, a handler can initialize a new WebSocket connection using NewGorillaWebsocketConnection and pass it to the controller:
To facilitate testing, a mock struct, WebsocketConnectionMock, is defined. This mock struct includes custom channels (e.g., socket and testSocket) for simulating the connection behavior, as well as a closed state to indicate if the connection has been terminated.
The
WebsocketController
accepts a WebSocket connection as an argument. To effectively unit test this, we need to mock the WebSocket connection. However, the Gorilla WebSocket library's connection interface is extensive and challenging to mock directly. To simplify testing, we can create a wrapper interface around the Gorilla WebSocket connection. This wrapper will be easier to mock, allowing us to simulate the WebSocket's behavior with custom channels that facilitate reading and writing data.The wrapper interface,
WebsocketConnection
, defines the core methods for testing:ReadJSON
,WriteJSON
, andClose
. Using this interface, we can verify that our controller sends and receives the expected data and behaves as intended.The code outline below shows how this can be implemented:
In our test setup, a handler can initialize a new WebSocket connection using
NewGorillaWebsocketConnection
and pass it to the controller:To facilitate testing, a mock struct,
WebsocketConnectionMock
, is defined. This mock struct includes custom channels (e.g.,socket
andtestSocket
) for simulating the connection behavior, as well as aclosed
state to indicate if the connection has been terminated.By using this setup, we can easily simulate data exchange through the mock channels and test that
WebsocketController
performs as expected.