I'm running some integration tests but then I noticed I was using the same AppBuilder across all the functions in the tests module in my login.rs. For eg.
// tests/login.rs
#[cfg(test)]
mod tests {
use actix_web::{test, web, App};
// use myproject::utils;
use super::*;
#[actix_web::test]
async fn test_login_get() {
let app = test::init_service(
App::new().service(
web::scope("login")
.service(
web::resource("club")
.route(web::get().to(utils::login::login_club))
.route(web::post().to(utils::login::login_club_post))
)
)
).await;
// Running some tests...
}
#[actix_web::test]
async fn test_login_post() {
let app = test::init_service(
// Would use the same AppBuilder in the above test function
).await;
// Running some tests...
}
}
I thought of probably using lazy_static but I'm unsure how to use it or if this would be the right use case.
I'd abstract app creation into a function and pass it to App::configure in your tests. I usually have that function public in my library so that I can use it when I create my production server and inside of my tests, great way to catch if you forgot to add a route somewhere.
// tests/login.rs
#[cfg(test)]
mod tests {
use actix_web::{test, web, App, ServiceConfig};
// use myproject::utils;
use super::*;
fn app(app: &mut ServiceConfig) {
app.service(
web::scope("login")
.service(
web::resource("club")
.route(web::get().to(utils::login::login_club))
.route(web::post().to(utils::login::login_club_post))
)
)
);
}
#[actix_web::test]
async fn test_login_get() {
let app = test::init_service(App::new().configure(app)).await;
// Running some tests...
}
#[actix_web::test]
async fn test_login_post() {
let app = test::init_service(App::new().configure(app)).await;
// Running some tests...
}
}