Crates.io | moq_lambda |
lib.rs | moq_lambda |
version | 0.2.1 |
source | src |
created_at | 2022-04-15 16:01:47.758068 |
updated_at | 2024-11-05 18:16:58.931196 |
description | Lambda generator |
homepage | |
repository | https://github.com/LazyMechanic/moq |
max_upload_size | |
id | 568544 |
size | 22,712 |
This library provides macro that provides mock struct generating that implements trait.
[dependencies]
moq = "0.4"
#[moq::automock]
trait Trait {
fn func(&self, arg: i64) -> String;
}
#[test]
fn test_ok() {
let mock = MockTrait::new()
.expect_func(|arg: i64| {
assert_eq!(arg, 42);
format!("Hello, {}", arg)
})
.expect_func(|arg: i64| {
assert_eq!(arg, -1);
format!("Hello again, {}", -1)
});
mock.func(42);
mock.func(-1);
}
#[test]
#[should_panic]
fn test_failed_extra_call() {
let mock = MockTrait::new()
.expect_func(|arg: i64| {
assert_eq!(arg, 42);
format!("Hello, {}", arg)
});
mock.func(42);
mock.func(-1); // Panic here
}
#[test]
#[should_panic]
fn test_failed_missing_call() {
let mock = MockTrait::new()
.expect_func(|arg: i64| {
assert_eq!(arg, 42);
format!("Hello, {}", arg)
})
.expect_func(|arg: i64| {
assert_eq!(arg, -1);
format!("Hello again, {}", -1)
});
mock.func(42);
// Panic here
}
async_trait
also works, but the main limitation is to specify the automock
macro above the async_trait
#[moq::automock]
#[async_trait::async_trait]
trait Trait {
async fn func(&self, arg: i64) -> String;
}
#[tokio::test]
async fn test_ok() {
let mock = MockTrait::new()
.expect_func(|arg: i64| async {
assert_eq!(arg, 42);
format!("Hello, {}", arg)
});
mock.func(42).await;
}
You can find more examples in the tests.
moq::mock!(..)
for generating mock struct for external traitmoq::lambda!