discordgo
discordgo copied to clipboard
Mock api package for integration testing.
Hi,
I would like to know about the options available for performing integration tests with discord servers. Something similar to slack test would be helpful. kindly suggest some pointers. thanks in advance
This is a great question. To my knowledge, I'm not aware of a Go project for mocking Discord's API. I have done some mocking of endpoints that I use, admittedly in a somewhat hacky way for my own bot project.
For example, I created a mock
package with a NewSession
function that returns a *discordgo.Session
with it's state pre-populated with with some mock objects like guilds, roles, channels, users/members, etc:
- https://github.com/ewohltman/ephemeral-roles/blob/master/internal/pkg/mock/session.go
- https://github.com/ewohltman/ephemeral-roles/blob/master/internal/pkg/mock/state.go
I also set it's internal *http.Client
to a client I've created with it's transport set to return back pre-defined responses to API requests:
- https://github.com/ewohltman/ephemeral-roles/blob/master/internal/pkg/mock/restClient.go
This mock package could definitely use some refactoring and is currently within my project's internal
directory so it can't be imported into other projects. A full mocking of Discord's API would probably end up being rather large and perhaps could benefit from being its own importable project in its own right.
Hope that helps!
FYI I made some changes to that mock
package and updated the links in the previous reply for posterity.
@ewohltman thanks for the prompt responses and efforts :+1:
FYI I just created this issue/PR in support of this: https://github.com/bwmarrin/discordgo/issues/939