From a8b01642c6b87a14f3fbf9da64b4707dab845d46 Mon Sep 17 00:00:00 2001 From: LasterAlex Date: Mon, 29 Jul 2024 17:51:58 +0300 Subject: [PATCH 1/2] Added testing field to README --- README.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/README.md b/README.md index 66e81040..e3b063fd 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,61 @@ async fn receive_location( [More examples >>](crates/teloxide/examples/) +## Testing + +A crate [`teloxide_tests`](https://github.com/LasterAlex/teloxide_tests) can be used to test your bots. + +An example of its usage: + +```rust,ignore +use teloxide::{ + dispatching::{dialogue::GetChatId, UpdateHandler}, + prelude::*, +}; + +fn handler_tree() -> UpdateHandler> { + dptree::entry().endpoint(|update: Update, bot: Bot| async move { + bot.send_message(update.chat_id().unwrap(), "Hello World!") + .await?; + Ok(()) + }) +} + +#[tokio::main] +async fn main() { + let bot = Bot::from_env(); + + Dispatcher::builder(bot, handler_tree()) + .enable_ctrlc_handler() + .build() + .dispatch() + .await; +} + +#[cfg(test)] +mod tests { + use super::*; + use teloxide_tests::{MockBot, MockMessageText}; + + #[tokio::test] + async fn test_hello_world() { + let message = MockMessageText::new().text("Hi!"); + let bot = MockBot::new(message, handler_tree()); + // Sends the message as if it was from a user + bot.dispatch().await; + + let responses = bot.get_responses(); + let message = responses + .sent_messages + .last() + .expect("No sent messages were detected!"); + assert_eq!(message.text(), Some("Hello World!")); + } +} +``` + +[More testing examples >>](https://github.com/LasterAlex/teloxide_tests/tree/master/examples) + ## Tutorials - [_"Migrating my family finance bot from Python to Rust (teloxide) because I am tired of exceptions (part 1)"_](https://trkohler.com/posts/i-migrated-my-family-finance-bot-from-python-to-rust-because-i-am-tired-of-exceptions/) by Troy Köhler. From a916670c6f7446a92261abf701e6110a9c633b46 Mon Sep 17 00:00:00 2001 From: LasterAlex Date: Mon, 29 Jul 2024 22:30:47 +0300 Subject: [PATCH 2/2] README update according to WaffleLapkin's comment --- README.md | 53 ++--------------------------------------------------- 1 file changed, 2 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index e3b063fd..6c19b5aa 100644 --- a/README.md +++ b/README.md @@ -288,58 +288,9 @@ async fn receive_location( ## Testing -A crate [`teloxide_tests`](https://github.com/LasterAlex/teloxide_tests) can be used to test your bots. +A community made crate [`teloxide_tests`](https://github.com/LasterAlex/teloxide_tests) can be used to test your bots. -An example of its usage: - -```rust,ignore -use teloxide::{ - dispatching::{dialogue::GetChatId, UpdateHandler}, - prelude::*, -}; - -fn handler_tree() -> UpdateHandler> { - dptree::entry().endpoint(|update: Update, bot: Bot| async move { - bot.send_message(update.chat_id().unwrap(), "Hello World!") - .await?; - Ok(()) - }) -} - -#[tokio::main] -async fn main() { - let bot = Bot::from_env(); - - Dispatcher::builder(bot, handler_tree()) - .enable_ctrlc_handler() - .build() - .dispatch() - .await; -} - -#[cfg(test)] -mod tests { - use super::*; - use teloxide_tests::{MockBot, MockMessageText}; - - #[tokio::test] - async fn test_hello_world() { - let message = MockMessageText::new().text("Hi!"); - let bot = MockBot::new(message, handler_tree()); - // Sends the message as if it was from a user - bot.dispatch().await; - - let responses = bot.get_responses(); - let message = responses - .sent_messages - .last() - .expect("No sent messages were detected!"); - assert_eq!(message.text(), Some("Hello World!")); - } -} -``` - -[More testing examples >>](https://github.com/LasterAlex/teloxide_tests/tree/master/examples) +[Some testing examples >>](https://github.com/LasterAlex/teloxide_tests/tree/master/examples) ## Tutorials