tucnak / telebot

Telebot is a Telegram bot framework in Go.
MIT License
3.91k stars 460 forks source link

Add test Context class for testing middlewares #712

Open freak12techno opened 2 months ago

freak12techno commented 2 months ago

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 commented 1 month ago

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