master-wiki/Read Later/What the Rust Book didn't tell you about testing....md
aleidk 784703b79b Update from obsidian - thinkpad
Affected files:
.obsidian/plugins/obsidian-omnivore/data.json
Read Later/Git Merge vs Rebase vs Squash ¿Qué estrategia debemos elegir-.md
Read Later/How Game Reviews Actually Affect You.md
Read Later/Let's Get Webby! 🦀 🕸️.md
Read Later/Use cases for Rust.md
Read Later/What the Rust Book didn't tell you about testing....md
Read Later/Why Signals Are Better Than React Hooks.md
books/Book/Caballo de troya.md
books/Book/En el nombre de la rosa.md
notes/coping mechanisms/Good Enough.md
notes/utils/Crear una nueva funcionalidad.md
notes/utils/Git.md
notes/utils/React.md
projects/personal-page-notes/List of Games.md
projects/quests.md
2023-11-05 13:26:21 -03:00

3.1 KiB
Raw Blame History

id title status tags date_added url_omnivore url_original
ad8a0732-733a-11ee-b2ab-13f1d6012322 What the Rust Book didn't tell you about testing... ARCHIVED
read-later
Newsletter
2023-10-25 10:30:35 https://omnivore.app/me/what-the-rust-book-didn-t-tell-you-about-testing-18b6707a120 https://omnivore.app/no_url?q=1fed5fd7-7706-46cf-9947-927b26a77112

What the Rust Book didn't tell you about testing...

Highlights

[!quote] We want to test get_user without making real database queries.

The solution is to mock the Database trait and assert execute_query is called with the correct query. But how?

We can use the mockall crate!

source

Original

The Rust Book has an entire chapter dedicated to testing, but it's missing a critical piece…

What happens when your code needs to make API calls or database queries?

Unit tests should be be fast, reliable, and deterministic.

We don't want to make expensive calls that might fail for various reasons.

Well here is some good news… we don't have to!

We can use mocking to substitute real objects for mock objects and assert certain expectations…

What's that? You want an example? Consider the following code…

trait Database { fn execute_query(&self, query: String); }

fn get_user(db: impl Database, id: i32) { let query = format!("SELECT * from Users where id={}", id); db.execute_query(query); } We want to test get_user without making real database queries.

The solution is to mock the Database trait and assert execute_query is called with the correct query. But how?

We can use the mockall crate!

Here is how we would test get_user

#[cfg(test)] use mockall::{automock, predicate::*};

#[cfg_attr(test, automock)] trait Database { fn execute_query(&self, query: String); }

fn get_user(db: impl Database, id: i32) { let query = format!("SELECT * from Users where id={}", id); db.execute_query(query); }

#[cfg(test)] mod tests { use super::*;

#[test]
fn get_user_executes_correct_query() {
    let mut mock_database = MockDatabase::new();
    mock_database.expect_execute_query()
        .with(eq("SELECT * from Users where id=22".to_owned()))
        .once()
        .returning(|_x| ());

    get_user(mock_database, 22);
}

} Boom! Now we have a unit test that's fast, reliable, and deterministic!

If you haven't seen my intro to testing in Rust video make sure to check it out!

Happy testing, and stay Rusty!