jbpratt / trivia

3 stars 4 forks source link

testing: mock websocket for future tests #22

Closed HoppenR closed 4 months ago

HoppenR commented 4 months ago

This allows using a mock websocket to stress test the trivia bot in the future. Intended to be used like this.

mockDialer := func(ctx context.Context, url string, opts *websocket.DialOptions) (WebsocketConn, *http.Response, error) {
    webMocket := new(MockWebSocketConn)
    // Add custom logic for what to return on different calls
    webMocket.On("Read", mock.Anything).Return(SOME_RETURN_VAL)
    return webMocket, nil, nil
}
bot, err := New(
    new(zap.SugaredLogger),
    mockDialer,
    "wss://chat2.strims.gg/ws", // or whatever...
    "placeholderToken",
    true,
    "leaderboardPage",
    "leaderboardIngress",
)
assert.Nil(t, err)
assert.NotNil(t, bot)
HoppenR commented 4 months ago

Can set up different return values from Read() like this.

webMocket.On("Read", mock.Anything).Return(someRet).Once()
webMocket.On("Read", mock.Anything).Return(otherRet).Once()