Item is gated behind the `mock` feature

I want to create mock data for my database unit tests. However, using #[cfg(feature = "mock")] throws the error:

error[E0432]: unresolved import `crate::tests::prepare_mock_data::prepare_mock_db`
  --> src/tests/user_test.rs:18:9
   |
18 |     use crate::tests::prepare_mock_data::prepare_mock_db;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `prepare_mock_db` in `tests::prepare_mock_data`
   |
note: found an item that was configured out
  --> src/tests/prepare_mock_data.rs:8:8
   |
8  | pub fn prepare_mock_db() -> DatabaseConnection {
   |        ^^^^^^^^^^^^^^^
note: the item is gated behind the `mock` feature
  --> src/tests/prepare_mock_data.rs:7:7
   |
7  | #[cfg(feature = "mock")]
   |       ^^^^^^^^^^^^^^^^
#[cfg(feature = "mock")]
pub fn prepare_mock_db() -> DatabaseConnection {

    let encrypted_pass = encrypt_pass("Password1&".to_string()).unwrap();

    MockDatabase::new(DatabaseBackend::Postgres)
        .append_query_results([
            vec![
                isumis_user::Model {
                    id: 1,
                    username: "Test".to_string(),
                    pass_hash: encrypted_pass.0,
                    salt: encrypted_pass.1,
                    class_id: None,
                    group_id: None,
                    user_role: Role::Student,
                    created_at: Default::default(),
                    modified_at: None,
                }
            ]
        ])
        .into_connection()
}
[features]
mock = ["sea-orm/mock"]

[[test]]
name = "user_test"
path = "src/tests/user_test.rs"
required-features = ["mock"]
[dependencies.sea-orm]
version = "1.1.0"
features = [
    "sqlx-postgres", 
    "runtime-async-std-native-tls", 
    "macros", 
    "runtime-async-std"
]

I want to use the feature on this one function because sea_orm does not allow to use mock for real databases.

The places you call or import prepare_mock_db must also be behind the "mock" feature gate. So in your src/tests/user_test.rs, try configuring out the import:

#[cfg(feature = "mock")]
use crate::tests::prepare_mock_data::prepare_mock_db;
2 Likes

It has to be everywhere where I need this feature....oof :slight_smile:

Maybe you can abstract every test that needs the mock feature into a single module and then feature-gate the module. Then you'd only have two places with #[cfg(feature = "mock")].

2 Likes