How to set expectation on a mocked method which is called inside another mocked method Rust

49 Views Asked by At

I am attempting to mock a method from a trait that returns another mock. I've tried creating two separate mock instances and having them return sequentially, but I've been unsuccessful. How can I solve this situation?

I am getting an error

no method named returning found for mutable reference &mut __get_account_query::Expectation in the current scope method not found in &mut Expectation

My handler


pub struct AppState {
    db: Box<dyn QueryDependency>,
}

#[post("/create")]
pub async fn create_account(
    data: web::Data<AppState>
) -> impl Responder {
     ..... 
     .....
    let result = data.db.get_account_query().create_account(&new_account).await;
                           ^^ the function I am trying to mock here
}  


#[actix_web::test]
async fn create_account_test() {
        let mock_db = MockQueryDependency::new();
        let mut mock_account_query = MockAccountQuery::new();

        mock_account_query
            .expect_create_account()
            .times(1)
            .returning(|_| Ok(Account {
                id: 1,
                name = "test".to_string(),
            }));
        
        mock_db.expect_get_account_query().times(1).returning(|| &mock_account_query);

        let mock_state = AppState {
            db: Box::new(mock_db),
        };
}

My traits

#[cfg(test)]
use mockall::automock;

#[cfg_attr(test, automock)]
pub trait QueryDependency: Send + Sync {
    fn get_account_query(&self) -> &dyn AccountQuery;
}

#[cfg_attr(test, automock)]
#[async_trait]
pub trait AccountQuery: Send + Sync {
    async fn create_account(&self, new_account: &NewAccount) -> Result<Account, sqlx::Error>;
}
0

There are 0 best solutions below