Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an interface on bot to allow mocking and unit testing #5

Open
christianrondeau opened this issue Aug 30, 2017 · 0 comments
Open

Comments

@christianrondeau
Copy link

TL;DR: I'd like a BotInterface :)

I'm trying out go-slackbot, and as a TDD user, I want to test my handlers. Right now, I have two choices; either I wrap the https://github.com/BeepBoopHQ/go-slackbot/blob/master/bot.go type in my own, or I create a SomethingHandler than then calls a SomethingHandlerImpl, which abstracts away the slackbot.Bot type.

I think a simpler solution would be to reference to an interface instead of a concrete struct type, and add a New overload that receives the bot. Something like:

func New(slackToken string) *BotInterface {
	b := &Bot{Client: slack.New(slackToken)}
	return b
}

interface BotInterface interface {
    // This is what would be passed around
}

type Bot struct {
    // Nothing else changes...
}

I could just make the interface myself, but then I also need to mock MessageHandler (because

type MessageHandler func(ctx context.Context, bot *Bot, msg *slack.MessageEvent)
references directly the concrete type), so it becomes complicated :)

The code might not be perfect, but you get the idea. I can then do:

func TestHelloHandler(t *testing.T) {
    ctx := context.TODO()
    bot := MyBotMock{}
    evt := slack.MessageEvent{}

    HelloHandler(ctx, &bot, &evt)

    // Some assertion...
}

What do you think?

flw-cn pushed a commit to flw-cn/go-slackbot that referenced this issue Jun 25, 2018
liamsorsby pushed a commit to liamsorsby/go-slackbot that referenced this issue Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant