discordgo icon indicating copy to clipboard operation
discordgo copied to clipboard

Mock api package for integration testing.

Open codenio opened this issue 4 years ago • 4 comments

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

codenio avatar May 01 '20 08:05 codenio

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!

ewohltman avatar May 01 '20 13:05 ewohltman

FYI I made some changes to that mock package and updated the links in the previous reply for posterity.

ewohltman avatar May 01 '20 14:05 ewohltman

@ewohltman thanks for the prompt responses and efforts :+1:

codenio avatar May 01 '20 14:05 codenio

FYI I just created this issue/PR in support of this: https://github.com/bwmarrin/discordgo/issues/939

ewohltman avatar May 21 '21 20:05 ewohltman