Giter Site home page Giter Site logo

Comments (2)

asomers avatar asomers commented on July 21, 2024

Sure there is. All you need to do is to export Client and MockClient from the same module. Perhaps like this:

mod mongodb {
    pub use ::mongodb::Client;
    #[cfg(test)]
    mock! {
        pub Client 
        ...
     }
}
#[double]
use mongodb::Client

from mockall.

gregdavisfromnj avatar gregdavisfromnj commented on July 21, 2024

Ah, okay... this was a bit of Rust I was not aware of being able to do with modules and redeclaring things that are really defined in another crate. My case is trickier though, and so to use another random mongodb type that is not mocked, for example ClientOptions below, I had to make the declaration like this to be able to resolve it:

use mongodb::options::ClientOptions;  // import to satisfy usage in main function

mod mongodb {
    pub use ::mongodb::Client;
    pub mod options {
        pub use ::mongodb::options::ClientOptions;
    }
    #[cfg(test)]  // import to satisfy usage in mock macro
    use mongodb::options::ClientOptions;
    #[cfg(test)]
    mock! {
        pub Client {
        ...
        pub fn with_options(options: ClientOptions) -> mongodb::error::Result<Self>;
        }
     }
}

#[double]
use mongodb::Client

fn main() {
    ...
    /* create a ClientOptions */
    /* create Client from with_options, passing it the ClientOptions */
    /* pass the Client to a cool_service::CoolService */ 
    ...
}

Now suppose the CoolService is in another file named cool_service.rs. The use statement to import mongodb::Client in that file, using the double macro, seems to require the crate:: prefix to make sure the MockClient is found in a different file for test build configuration. Like this:

// cool_service.rs

#[double]
use crate::mongodb::Client;

Going back to the hypothetical first file where main is defined, I can seem to use either use crate::mongodb::Client; or use mongodb::Client; for the import because they seem to mean the same thing there.

And finally, in case anyone else ever goes down this path like me... I was able to move the contents of the mongodb module defined above to its own module as a file name mongodb.rs, and then replace the module definition with a declaration of the module like mod mongodb;. Because MockClient is actually used in that file during the test configuration, and not mongodb::Client, I needed to annotate the pub use ::mongodb::Client; line with #[cfg(not(test))] to avoid an unused-import warning during normal build.

Thanks for the quick response!

from mockall.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.