-
Notifications
You must be signed in to change notification settings - Fork 185
Closed
Description
I'm currently writing unit tests for a web service using the Rust MongoDB Driver. As part of these tests, I aim to mock both input and return data without actually querying a database.
However, MongoDB marks its operation result structs as #[non_exhaustive]. This prevents direct instantiation when mocking these structs.
Wouldn't it be beneficial to allow users to handle potential versioning issues themselves in the case of operation results structs?
#[cfg(test)]
mod tests {
use mongodb::results::UpdateResult;
use crate::{
modules::user::{repository::MockUserRepository, service::UserService},
tests::unit::common::get_user_to_test,
};
#[tokio::test]
async fn check_if_set_session_id_in_user() {
let mut mock_repo = MockUserRepository::new();
let mock_update_result = UpdateResult { //cannot create non-exhaustive struct using struct expression
matched_count: 1,
modified_count: 1,
upserted_id: None,
};
mock_repo
.expect_update_one()
.returning(move |_, _, _| Ok(mock_update_result));
let user_service = UserService::new(mock_repo);
let mut user = get_user_to_test();
user.session_id = None;
assert!(user.session_id.is_none());
let result = user_service.check_or_add_session(&mut user).await;
assert!(result.is_ok());
assert!(user.session_id.is_some());
}
}
Metadata
Metadata
Assignees
Labels
No labels