telebot icon indicating copy to clipboard operation
telebot copied to clipboard

Add test Context class for testing middlewares

Open freak12techno opened this issue 1 year ago • 1 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.

freak12techno avatar Jul 06 '24 14:07 freak12techno

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

freak12techno avatar Jul 09 '24 10:07 freak12techno

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

freak12techno avatar Nov 06 '24 09:11 freak12techno