Crate teremock

Crate teremock 

Source
Expand description

§teremock - Production-grade Mock Bot for Teloxide Integration Testing

A high-performance mock bot for integration testing teloxide bots with an actual fake server.

§Key Features

  • Persistent Server Architecture: Server starts once and is reused across all dispatches
  • Stack Overflow Prevention: Uses tokio task spawn per dispatch to prevent stack buildup
  • Black-Box Testing: No dialogue state manipulation - tests interact only through the bot interface
  • Rich Response Inspection: Comprehensive access to all bot API responses

§Quick Start

use teloxide::{
    dispatching::{UpdateFilterExt, UpdateHandler},
    prelude::*,
};

type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;

async fn hello_world(bot: Bot, message: Message) -> HandlerResult {
    bot.send_message(message.chat.id, "Hello World!").await?;
    Ok(())
}

fn handler_tree() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
    dptree::entry().branch(Update::filter_message().endpoint(hello_world))
}

#[cfg(test)]
mod tests {
    use super::*;
    use teremock::{MockBot, MockMessageText};

    #[tokio::test]
    async fn test_hello_world() {
        let mut bot = MockBot::new(MockMessageText::new().text("Hi!"), handler_tree()).await;
        bot.dispatch().await;
        let message = bot.get_responses().sent_messages.last().unwrap();
        assert_eq!(message.text(), Some("Hello World!"));
    }
}

§Architecture

teremock is designed for production-grade integration testing with:

  1. Persistent Server: Unlike the original teloxide_tests where each dispatch creates a new server, teremock keeps the server alive across all dispatches. This provides 15-30x faster test execution (2s vs 30-60s for 50+ dispatches).

  2. Tokio Task Isolation: Each dispatch runs in a separate tokio task with a fresh 2MB stack. This prevents stack overflow issues that occur when handler trees are cloned across many sequential dispatches.

  3. Black-Box Testing Philosophy: Tests should interact only through the bot interface (messages, callbacks, commands). There’s no dialogue state manipulation API - state changes happen naturally through the handler tree.

§Supported Endpoints

  • /AnswerCallbackQuery
  • /DeleteMessage
  • /DeleteMessages
  • /EditMessageText
  • /EditMessageReplyMarkup
  • /EditMessageCaption
  • /GetFile
  • /SendMessage
  • /SendDocument
  • /SendPhoto
  • /SendVideo
  • /SendAudio
  • /SendVoice
  • /SendVideoNote
  • /SendAnimation
  • /SendLocation
  • /SendVenue
  • /SendContact
  • /SendDice
  • /SendPoll
  • /SendSticker
  • /SendChatAction
  • /SendMediaGroup
  • /SendInvoice
  • /PinChatMessage
  • /UnpinChatMessage
  • /UnpinAllChatMessages
  • /ForwardMessage
  • /CopyMessage
  • /BanChatMember
  • /UnbanChatMember
  • /RestrictChatMember
  • /SetMessageReaction
  • /SetMyCommands
  • /GetMe

§Migration from teloxide_tests

The main API differences:

// OLD (teloxide_tests):
let mut bot = MockBot::new(MockMessageText::new().text("Hi!"), handler_tree());
bot.dispatch().await;

// NEW (teremock):
let mut bot = MockBot::new(MockMessageText::new().text("Hi!"), handler_tree()).await;
bot.dispatch().await;
// Note: `new()` is now async because it starts the server immediately

Key differences:

  • new() is now async (starts the server immediately)
  • No set_state() / get_state() methods (black-box testing)
  • Server persists across dispatches (much faster)
  • Works with default 2MB stack (no custom thread builder needed)
  • No global lock needed (server is persistent per MockBot instance)

Re-exports§

pub use server::Responses;

Modules§

chat
chat_full_info
message
message_common
queries
server
A fake telegram bot API for testing purposes. Read more in teremock crate.
update

Structs§

DistributionKey
A key that defines the parallelism of updates
MockBot
A mocked bot that sends requests to the fake server.
MockCallbackQuery
MockChannelChat
MockChatFullInfoChannel
MockChatFullInfoGroup
MockChatFullInfoPrivate
MockChatFullInfoSupergroup
MockChatPhoto
MockEditedMessage
MockGroupChat
MockLinkPreviewOptions
MockLocation
MockMe
MockMessageAnimation
MockMessageAudio
MockMessageContact
MockMessageDice
MockMessageDocument
MockMessageGame
MockMessageInvoice
MockMessageLocation
MockMessageMigrationFromChat
MockMessageMigrationToChat
MockMessageNewChatMembers
MockMessagePhoto
MockMessagePoll
MockMessageSticker
MockMessageText
MockMessageVenue
MockMessageVideo
MockMessageVideoNote
MockMessageVoice
MockPhotoSize
MockPrivateChat
MockSupergroupChat
MockUpdatePoll
MockUser
MockVideo

Traits§

IntoChatId
Trait for types that can be converted to ChatId.
IntoMessageId
Trait for types that can be converted to MessageId.
IntoUpdate
IntoUserId
Trait for types that can be converted to UserId.

Type Aliases§

HandlerError
Error type alias commonly used with handler trees