tucnak/telebot

Add test Context class for testing middlewares

Closed this issue · 2 comments

I use telebot in a few of my apps, and I have a bunch of functions similar to these I want to test:

func (reporter *Reporter) HandleHelp(c tele.Context) error {

So I want to construct tele.Context instance to pass it as an argument, however nativeContext is not exporter, and to implement tele.Context I need to create a new struct that would implement all of these methods (more than 20).
Would be lovely if telebot would include some implementation of tele.Context that can be used in tests, that would not do any real requests but would return some data that might be configurable.
Something like this would be ideal:

ctx := tele.TestContext{
    Sender: { Username: "test" },
    Reply: { Error: errors.New("some error"),
}
reporter.HandleHelp(ctx)
// add some asserts that it actually worked correctly

Also pretty sure this might be handy in using the telebot itself.

@demget do you mind if I add this by making a PR?

Nevermind, I figured it out, you can just call bot.NewContext(tele.Update{}) to generate it.