You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写Actix Web单元测试时触发E0618错误:expected function

问题原因

当你给函数加上#[get("/ping")]宏后,Actix Web会将原函数包装成一个路由处理结构体,而非保留原本的函数签名。此时health_check已不再是接受HttpRequest参数的普通函数,直接传入req调用自然会触发E0618错误。

解决方案

有两种常见解决方式,按需选择:

1. 提取业务逻辑单独测试

若仅需验证处理函数的核心逻辑,可把业务代码从路由处理函数中抽离,直接测试纯逻辑函数:

use actix_web::{get, web, Responder, Result};
use serde::Serialize;

#[derive(Serialize, Debug)]
struct PingResponse {
    status: String,
}

// 抽离核心业务逻辑
fn build_ping_response() -> PingResponse {
    PingResponse {
        status: "alive".to_string(),
    }
}

#[get("/ping")]
async fn health_check() -> Result<impl Responder> {
    let resp = build_ping_response();
    Ok(web::Json(resp))
}

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

    #[test]
    fn test_ping_response_logic() {
        let resp = build_ping_response();
        assert_eq!(resp.status, "alive".to_string());
    }
}

2. 使用Actix测试框架模拟请求

若需模拟真实HTTP请求流程,用Actix提供的测试工具调用整个服务:

use actix_web::{get, web, Responder, Result, App};
use serde::Serialize;

#[derive(Serialize, Debug)]
struct PingResponse {
    status: String,
}

#[get("/ping")]
async fn health_check() -> Result<impl Responder> {
    let resp = PingResponse {
        status: "alive".to_string(),
    };
    Ok(web::Json(resp))
}

#[cfg(test)]
mod tests {
    use super::*;
    use actix_web::{test, http::StatusCode};

    #[actix_web::test]
    async fn test_ping_ok() {
        // 创建测试用App实例并注册路由
        let app = test::init_service(
            App::new().service(health_check)
        ).await;

        // 构建GET请求并发送
        let req = test::TestRequest::get().uri("/ping").to_request();
        let resp = test::call_service(&app, req).await;

        // 验证响应状态码
        assert_eq!(resp.status(), StatusCode::OK);

        // 解析响应体并验证内容
        let body: PingResponse = test::read_body_json(resp).await;
        assert_eq!(body.status, "alive".to_string());
    }
}
关键说明
  • 路由宏(#[get]/#[post]等)会修改原函数类型,不能直接当作普通函数调用。
  • 测试核心逻辑优先用第一种方案,简单直接;测试完整HTTP流程用第二种方案,更贴近真实场景。

内容的提问来源于stack exchange,提问作者Pavel Byles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 06:30:50