Comments (7)
That message is telling you that the as_any
method's return value is not Send
. Try adding a + Send
trait bound to it.
from mockall.
automock does work with async_trait. For example, see https://github.com/asomers/mockall/blob/master/mockall/tests/automock_async_trait.rs . The main thing to be aware of is that the automock attribute must precede the async_trait attribute.
from mockall.
Okay, my program does not compile if I do it. Maybe somebody could assist to fix the example above.
from mockall.
Maybe you could post the error message?
from mockall.
Of course:
error: future cannot be sent between threads safely
--> src/db/mod.rs:30:1
|
30 | #[automock]
| ^^^^^^^^^^^ future created by async block is not `Send`
|
note: captured value is not `Send` because `&mut` references cannot be sent unless their referent is `Send`
--> src/db/mod.rs:45:27
|
45 | async fn connect(&mut self, config: &Config) -> Result<(), mongodb::error::Error>;
| ^^^^ has type `&mut MockDatabaseService<T>` which is not `Send`, because `MockDatabaseService<T>` is not `Send`
= note: required for the cast from `Pin<Box<{async block@src/db/mod.rs:30:1: 30:12}>>` to `Pin<Box<(dyn futures::Future<Output = Result<(), mongodb::error::Error>> + std::marker::Send + 'async_trait)>>`
= note: this error originates in the attribute macro `automock` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
|
32 | pub trait DatabaseService<T: model::Element + std::marker::Send>: Debug + Sync + Send {
| +++++++++++++++++++
from mockall.
Did you try following the compiler's advice?
from mockall.
After fullfilling the suggestions step for step I arrived at:
#[cfg(test)]
use mockall::{automock, mock, predicate::*};
#[cfg_attr(test, automock)]
#[async_trait]
pub trait DatabaseService<T: model::Element + std::marker::Send + std::marker::Sync>:
Debug + Sync + Send
// ...
With error message:
error[E0277]: `(dyn std::any::Any + 'static)` cannot be sent between threads safely
--> src/db/mod.rs:30:11
|
30 | pub trait DatabaseService<T: model::Element + std::marker::Send + std::marker::Sync>:
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn std::any::Any + 'static)` cannot be sent between threads safely
|
= help: the trait `std::marker::Send` is not implemented for `(dyn std::any::Any + 'static)`, which is required by `MockDatabaseService<T>: std::marker::Send`
= note: required for `Unique<(dyn std::any::Any + 'static)>` to implement `std::marker::Send`
note: required because it appears within the type `Box<(dyn std::any::Any + 'static)>`
--> /Users/titan/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/boxed.rs:197:12
|
197 | pub struct Box<
| ^^^
note: required because it appears within the type `__as_any::Rfunc<T>`
--> src/db/mod.rs:28:18
|
28 | #[cfg_attr(test, automock)]
| ^^^^^^^^
note: required because it appears within the type `__as_any::Expectation<T>`
--> src/db/mod.rs:28:18
|
28 | #[cfg_attr(test, automock)]
| ^^^^^^^^
= note: required for `Unique<__as_any::Expectation<T>>` to implement `std::marker::Send`
note: required because it appears within the type `alloc::raw_vec::RawVec<__as_any::Expectation<T>>`
--> /Users/titan/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/raw_vec.rs:69:19
|
69 | pub(crate) struct RawVec<T, A: Allocator = Global> {
| ^^^^^^
note: required because it appears within the type `Vec<__as_any::Expectation<T>>`
--> /Users/titan/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:398:12
|
398 | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
| ^^^
note: required because it appears within the type `__as_any::Expectations<T>`
--> src/db/mod.rs:28:18
|
28 | #[cfg_attr(test, automock)]
| ^^^^^^^^
note: required because it appears within the type `MockDatabaseService_DatabaseService_13065831140148901594<T>`
--> src/db/mod.rs:30:11
|
30 | pub trait DatabaseService<T: model::Element + std::marker::Send + std::marker::Sync>:
| ^^^^^^^^^^^^^^^
note: required because it appears within the type `MockDatabaseService<T>`
--> src/db/mod.rs:30:11
|
30 | pub trait DatabaseService<T: model::Element + std::marker::Send + std::marker::Sync>:
| ^^^^^^^^^^^^^^^
note: required by a bound in `DatabaseService`
--> src/db/mod.rs:31:20
|
30 | pub trait DatabaseService<T: model::Element + std::marker::Send + std::marker::Sync>:
| --------------- required by a bound in this trait
31 | Debug + Sync + Send
| ^^^^ required by this bound in `DatabaseService`
= note: this error originates in the attribute macro `automock` (in Nightly builds, run with -Z macro-backtrace for more info)
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 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
- 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
- `#[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 7
- Is it possible to mock a trait method that has no return? HOT 2
- Can't `send` a Mock Object that returns Future HOT 1
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.