Comments (3)
No, you can't do that. Module declarations don't get expanded when proc macros get evaluated, so as your code is written automock
can't see the contents of some_module at all. It thinks its an empty module. Instead, try like this:
main.rs:
#![feature(proc_macro_hygiene)]
#[cfg(test)]
use mockall::predicate::eq;
pub mod some_module;
use some_module::free_function;
fn my_func() {
free_function(7);
}
fn main() {
}
#[test]
fn some_test() {
let context = some_module::free_function_context();
context.expect().times(1).with(eq(7)).returning(|_x| ());
my_func();
}
some_module.rs:
use cfg_if::cfg_if;
#[cfg(test)]
use mockall::automock;
#[cfg_attr(test, automock)]
pub mod inner {
pub fn free_function(_arg: i32) {}
}
cfg_if! {
if #[cfg(test)] {
pub use mock_inner::*;
} else {
pub use inner::*;
}
}
I'll see if I can add a compiler error for using automock that way.
from mockall.
Great, thank you. That works as is, but extending a little further I'm stuck on the following scoping issue. I can open as a separate issue if that would be better.
some_module.rs:
use cfg_if::cfg_if;
#[cfg(test)]
use mockall::automock;
pub struct SomeStruct {}
#[cfg_attr(test, automock)]
pub mod inner {
use super::SomeStruct;
pub fn free_function(_arg: SomeStruct) {}
}
cfg_if! {
if #[cfg(test)] {
pub use mock_inner::*;
} else {
pub use inner::*;
}
}
main.rs:
#![feature(proc_macro_hygiene)]
#[cfg(test)]
use mockall::predicate::*;
pub mod some_module;
use some_module::*;
fn my_func() {
let s = some_module::SomeStruct {};
some_module::free_function(s);
}
fn main() {
}
#[test]
fn some_test() {
let context = some_module::free_function_context();
context.expect().times(1).with(always()).returning(|_x| ());
my_func();
}
gives:
error[E0412]: cannot find type `SomeStruct` in this scope
--> src/some_module.rs:10:32
|
10 | pub fn free_function(_arg: SomeStruct) {}
| ^^^^^^^^^^ not found in this scope
...
It works if some_module.rs is updated to only uses absolute paths:
#[cfg_attr(test, automock)]
pub mod inner {
pub fn free_function(_arg: crate::some_module::SomeStruct) {}
}
from mockall.
Yes, that's different. Please open a separate issue for it, and I'll suggest some other workarounds.
from mockall.
Related Issues (20)
- Remove ...
- Problem with mocking a trait which uses &Self HOT 1
- Mockall Double of struct in external crate HOT 2
- 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 6
- 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
- Mocking a function taking an FnMut closure incompatible with concretize HOT 2
- How to create a "dummy" mock where no expectations are required HOT 4
- Mocking an async trait with generics HOT 7
- `#[derive(Clone)]` is not generating the implementation HOT 2
- extern C functions need to be C-unwind in Rust 1.81.0
- Mocking a Generic Trait with mock! Macro: 'Type Parameter Not Constrained' Error HOT 3
- Define mocks in #[cfg(test)] HOT 9
- Is it possible to mock a trait method that has no return? HOT 2
- Can't `send` a Mock Object that returns Future HOT 2
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.