テストの作成

テストの重要性

テストを作成することは、エンジニアとしての責任ある態度であり、安心して眠るための秘訣でもあります。包括的なテストスイートは、コード品質を向上させ、回帰エラーを防止するだけでなく、デプロイ後も安眠をもたらします。同僚が深夜にシステムクラッシュのアラートを受け取る中、あなたのアプリケーションが揺るぎなく安定しているとき、それがテストがもたらす平穏と自信です。

Salvoのテストツール

Salvoが提供するtestモジュールは、Salvoプロジェクトのテストを支援します。

最新ドキュメント

簡単な例:

use salvo::prelude::*;

#[handler]
async fn hello_world() -> &'static str {
    "Hello World"
}

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt().init();

    tracing::info!("Listening on http://127.0.0.1:8698");
    let acceptor = TcpListener::new("127.0.0.1:8698").bind().await; Server::new(acceptor).serve(route()).await;
}

fn route() -> Router {
    Router::new().get(hello_world)
}

#[cfg(test)]
mod tests {
    use salvo::prelude::*;
    use salvo::test::{ResponseExt, TestClient};

    #[tokio::test]
    async fn test_hello_world() {
        let service = Service::new(super::route());

        let content = TestClient::get(format!("http://127.0.0.1:8698/"))
            .send(&service)
            .await
            .take_string()
            .await
            .unwrap();
        assert_eq!(content, "Hello World");
    }
}