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:
-
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).
-
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.
-
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 immediatelyKey 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§
- Distribution
Key - A key that defines the parallelism of updates
- MockBot
- A mocked bot that sends requests to the fake server.
- Mock
Callback Query - Mock
Channel Chat - Mock
Chat Full Info Channel - Mock
Chat Full Info Group - Mock
Chat Full Info Private - Mock
Chat Full Info Supergroup - Mock
Chat Photo - Mock
Edited Message - Mock
Group Chat - Mock
Link Preview Options - Mock
Location - MockMe
- Mock
Message Animation - Mock
Message Audio - Mock
Message Contact - Mock
Message Dice - Mock
Message Document - Mock
Message Game - Mock
Message Invoice - Mock
Message Location - Mock
Message Migration From Chat - Mock
Message Migration ToChat - Mock
Message NewChat Members - Mock
Message Photo - Mock
Message Poll - Mock
Message Sticker - Mock
Message Text - Mock
Message Venue - Mock
Message Video - Mock
Message Video Note - Mock
Message Voice - Mock
Photo Size - Mock
Private Chat - Mock
Supergroup Chat - Mock
Update Poll - Mock
User - Mock
Video
Traits§
- Into
Chat Id - Trait for types that can be converted to
ChatId. - Into
Message Id - Trait for types that can be converted to
MessageId. - Into
Update - Into
User Id - Trait for types that can be converted to
UserId.
Type Aliases§
- Handler
Error - Error type alias commonly used with handler trees