I created a module/crate for all the database access that I need to do for an application.
pub struct Db {
client: db_client
}
impl Db {
pub fn new(client: db_client) -> Self {
return Self {
client: client
}
}
pub async fn func_1(&self, params: type) {
...
}
}
#[cfg(test)]
mod tests {
#[actix_rt::test]
async fn test_func_1() {
// create client object
// create Db object
// call desired function
...
}
}
For every test I have to create the object anew before I can actually test the desired function. There is no setup or teardown function in rust tests. How do you avoid recreating the client connection in every test? By the way, obtaining the client connection requires a async/await call. I tried the solutions in this post: How to run setup code before any tests run in Rust? but I couldn't get it to work.