1

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.

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
ferd tomale
  • 845
  • 7
  • 20
  • Perhaps the question is why you couldn't the other question's answers to work? – Jared Farrish Oct 13 '22 at 13:39
  • part of the answer was I needed to use async functions and the setup/teardown sample code didn't work with those. I tried using the lazy_static but the same issue with async functions. – ferd tomale Oct 26 '22 at 07:22

0 Answers0