How to make multiple mock types with different TypeId when mocking a trait #392
Answered
by
asomers
richardzone
asked this question in
Questions
-
I'd like to test some code which uses #[automock]
trait Task {}
// In real implementation code I have different types of Tasks stored within a Vec<Box<dyn Task>>
// And I'd perform "std::any"-based operations on these tasks
struct A;
impl Task for A {}
struct B;
impl Task for B {}
// What I'd like to achieve is to make the following test pass:
#[test]
fn test() {
let task_a = MockTask::new();
let task_b = AnotherMockTask::new();
assert_ne!(task_a.type_id(), task_b.type_id());
assert_ne!(TypeId::of::<MockTask>(), TypeId::of::<AnotherMockTask>());
} |
Beta Was this translation helpful? Give feedback.
Answered by
asomers
Jul 5, 2022
Replies: 1 comment
-
Sure. It's easy. You can only use #[automock]
trait Task{}
mock!{
pub OtherTask{}
impl Task for OtherTask{}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
richardzone
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sure. It's easy. You can only use
automock
once, but you can usemock!
as much as you like.