Comments (6)
Could you please post a complete example?
from mockall.
A more complete example of how the create i'm attempting to mock is used or how i would use it in a test? Or both?
from mockall.
A minimal example that demonstrates your problem. The one you posted above is incomplete.
from mockall.
Please let me know if any further clarification is required. Thank you!
use std::io::Error;
use tokio_modbus::prelude::*;
use mockall::*;
use async_trait::async_trait;
mock! {
#[allow(dead_code)]
#[derive(Debug)]
Context {}
#[async_trait]
impl Client for Context {
async fn call(&mut self, request: Request<'_>) -> Result<Response, std::io::Error> {}
}
impl SlaveContext for Context {
fn set_slave(&mut self, slave: Slave) {}
}
}
fn main() -> Result<(), Error> {
let mock = MockContext::new();
let v = vec![1234u16];
mock.returning(v);
let reg = mock.read_input_registers(1u16, 1);
assert_eq!(reg, v);
Ok(())
}
from mockall.
So Mockall can't yet handle the anonymous lifetime in an argument position. It could with some extra work. But it usually doesn't need to, because when using mock!
the user always has the option to give the lifetime a name. In this case, it's hard for you to name the lifetime, because async_trait
adds some extra lifetimes. So in order to make this work, you have to use cargo expand
to see what async_trait
is actually doing. But once you've done that, you can easily make it work, like this:
use async_trait::async_trait;
use mockall::*;
use std::io::Error;
use tokio_modbus::prelude::*;
use core::future::Future;
use std::pin::Pin;
mock! {
#[allow(dead_code)]
#[derive(Debug)]
Context {}
#[async_trait]
impl Client for Context {
fn call<'life0, 'life1, 'async_trait>(
&'life0 mut self,
request: Request<'life1>,
) -> Pin<
Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>
>
where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
{}
}
impl SlaveContext for Context {
fn set_slave(&mut self, slave: Slave) {}
}
}
from mockall.
Thank you for that explanation. I'll look into cargo expand, I saw it in the playground but hadn't installed the tool locally. Appreciate the your knowledge. That does assist the compiler. I'll keep this in mind for the future!
from mockall.
Related Issues (20)
- 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
- version 0.13 really from you? HOT 1
- Feature request: Allow manually verifying expectations without requiring mutability HOT 9
- How to correctly define associated types? HOT 2
- Creating a mock with multiple traits fails if traits share a name HOT 2
- External functions seem to be "overwritten" by mocks when exported HOT 5
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.