-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
capture argument values for further assertions #366
Comments
Can you give an example of how you would use this feature? |
let my_captor = captor::<ComplexStruct>();
// prime the mock
my_mock.expect_method().with(function(my_captor.capture())).returning(...);
// ... do your test
// assertions
my_mock.checkpoint();
let actual_value = my_captor.value().unwrap();
assert_eq!(actual_value.some_field, "some value");
// etc., test as many or as few fields as you need Actually, if my_mock.expect_method().with(my_captor.capture()).returning(...); |
Ok, I see how this would be useful. And it's one of the rare feature requests that doesn't require changing the proc macros. Would you like to try submitting a PR to implement it? |
sure, I'll give it a shot. |
Hi guys, I wanted to create a similar feature request and then I realized someone already did. Really too bad the PR was closed because the source branch was deleted... I was thinking if that could be possible to implement // Arrange
let mut sent_links: Vec<String> = Vec::new();
let mut email_client_mock = MockEmailClient::new();
email_client_mock
.expect_send_email()
.withf_mut(
move |recipient: &str, subject: &str, text_content: &str| {
assert_eq!(recipient, "foo@domain.com");
assert_eq!(subject, "Welcome !");
let confirmation_link = extract_single_link(text_content);
sent_links.push(confirmation_link.to_owned());
true
},
)
.once()
.returning(|_, _, _| Ok(()));
// Whatever
// Act
[...]
// Assert
let first_link = Url::parse(sent_links.first().unwrap()).unwrap();
let response = reqwest::get(first_link).await.unwrap();
assert_eq!(response.status().as_u16(), 200); Sorry if my code might seem a bit stupid (or wrong), once again, I'm a beginner in Rust. I just hope my use case is clear enough. In that example I can't call Would that be possible to implement such functions? I agree with @albx79, being able to capture arguments for further processing is a MUST HAVE feature for a mocking library. I've been playing with Thanks for your answer |
I agree that But without adding new features, I also have a few suggestions for how to improve your current tests:
|
Hi Alan, Thanks a lot for your kind and detailed reply, this is very much appreciated 🙏
|
@asomers What's the reason |
You probably need an
No, we really can't, because internally, the closure needs to be stored in a structure that is |
When mocking a method that takes complex structures as parameters, it is common to prime the mock to capture that argument, and subsequently assert on it. See e.g. https://site.mockito.org/javadoc/current/org/mockito/ArgumentCaptor.html.
I was able to achieve the same with little code:
(maybe it would be more flexible to use
Vec<T>
instead ofOption<T>
).However, I believe that this is basic functionality, expected to be provided by a mocking library.
Captor
should just be anotherPredicate
.Thanks.
The text was updated successfully, but these errors were encountered: