Comments (2)
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.
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)
- Relax 'static requirement for generic arguments for creating stucts, but not using them HOT 1
- Anonymous lifetime in trait method HOT 6
- Mocking an internal struct with external dependency HOT 7
- Capturing arguments from returning HOT 2
- Struggling to mock trait objects and make assertions on functions that take ownership of mock trait object HOT 4
- Mock trait with methods that return iterators HOT 8
- Mock Trait that is a composition of two other traits HOT 2
- Struct with different traits that has identically named methods HOT 1
- Can't apply `automock` to traits with default method implementions marked `#[inline]` HOT 1
- Remove ...
- Problem with mocking a trait which uses &Self HOT 1
- mocking struct & mockall_double::double not working as expected HOT 10
- Mocking with tokio::sync::Mutex, confused for std::sync::Mutex HOT 4
- Feature request: create a `spy!` macro to provide a transparent mock but that can be asserted upon. HOT 6
- Feature request: support for `const generics` in `mock!` and `automock` HOT 4
- Mocking Default Trait Method Implementations HOT 4
- How to bypass a mock for a particular test and call the original function HOT 3
- automock fails for method taking Option<&u32> HOT 5
- MSRV 1.77.0 TODO
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockall.