You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hi , our project is using Actix, which is really fast, however, when writing tests, I found there are many duplications in our tests code. espcially the "init server" code, e.g.
#[actix_rt::test]
async fn test_1() {
let AppData {
db_pool,
redis_actor,
market,
orders,
last_price,
} = generate_test_data();
let mut app_service = test::init_service(
App::new()
.wrap(Logger::new("%r %s time_consuming: %Dms"))
.data(db_pool.clone())
.data(redis_actor.clone())
.data(market.clone())
.app_data(orders.clone())
.app_data(last_price.clone())
.configure(init_routes),
)
.await;
let ref_db_connection = &db_pool.get().unwrap();
// test code begins ......
}
#[actix_rt::test]
async fn test_2() {
let AppData {
db_pool,
redis_actor,
market,
orders,
last_price,
} = generate_test_data();
let mut app_service = test::init_service(
App::new()
.wrap(Logger::new("%r %s time_consuming: %Dms"))
.data(db_pool.clone())
.data(redis_actor.clone())
.data(market.clone())
.app_data(orders.clone())
.app_data(last_price.clone())
.configure(init_routes),
)
.await;
let ref_db_connection = &db_pool.get().unwrap();
/* real test code begins ... */
}
I know there are many test frameworks such as JUnit, Rspec support before/after hooks, which make the code DIY and clean. In the code above, the JUnit way is to extract the "init service code" to before hook,
hi , our project is using Actix, which is really fast, however, when writing tests, I found there are many duplications in our tests code. espcially the "init server" code, e.g.
I know there are many test frameworks such as JUnit, Rspec support before/after hooks, which make the code DIY and clean. In the code above, the JUnit way is to extract the "init service code" to
before hook
,I have googled around and found this post: https://medium.com/@ericdreichert/test-setup-and-teardown-in-rust-without-a-framework-ba32d97aa5ab I don't know if this is the official way.
so, how to do
before/after
hooks in Actix/Rust ?thanks a lot !
The text was updated successfully, but these errors were encountered: