You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to mock a service that is supposed to be called multiple times and fail, and then succeed at the Nth time. Since these are retries the requests should be the same, so there's no way to distinguish them by parameters or by path/body.
#[test]fndelete_mock_test(){let server = MockServer::start();letmut m1 = server.mock(|when, then| {
when.method(GET).path("/test");
then.status(500);});letmut m2 = server.mock(|when, then| {
when.method(GET).path("/test");
then.status(200);});let response = get(&format!("http://{}:{}/test",
server.host(),
server.port())).unwrap();assert_eq!(response.status(),500);// As long as both m1 and m2 exist, m1 will take precedence over m2. // Let's delete m1 so we can respond with a successful response.
m1.delete();let response = get(&format!("http://{}/test", server.address())).unwrap();assert_eq!(response.status(),200);}
Not quite. I want to test a utility function that does the retries by itself, so I don't get a chance to intervene after the first N retries... It would be great to have a "match_at_most(N)" helper on the when clause, or even provide a callback to control what happens based on a variable
Reacted by Nathan Henrie, Robert Chambers, Andrey Letenkov and Will Chandler
Interesting idea ... OK, let's keep this issue open and I'll try to get a PR for it after the next release (it's a big change, so it makes sense to wait a bit).
I have this same exact need. With <0.8 I was able to use global variables and atomics to track the count in the .match(), but as of 0.8, I can no longer do that with .is_true() since all mocks are checked causing issues with the counting. Since I need to send back a 5XX error with a payload I have to run with two mocks and that's double counting the global variable in my case. Would love to see traction on this feature.
Activity
alexliesenfeld commentedon Jan 7, 2024
Hi @jamesperez2005 , thanks for reaching out!
Do you think delete will help you here?
jamesperez2005 commentedon Jan 8, 2024
Not quite. I want to test a utility function that does the retries by itself, so I don't get a chance to intervene after the first N retries... It would be great to have a "match_at_most(N)" helper on the
whenclause, or even provide a callback to control what happens based on a variablealexliesenfeld commentedon May 3, 2024
Interesting idea ... OK, let's keep this issue open and I'll try to get a PR for it after the next release (it's a big change, so it makes sense to wait a bit).
Cleanup partially imported disks on CTRL+C
Add delete_after_n method to When
Cleanup partially imported disks on CTRL+C
mothran commentedon Oct 31, 2024
Until #108 lands here is a (ugly) work around to mock failing the first request:
Its not pretty but is a reasonable workaround.
vtereso commentedon Apr 16, 2025
+1 on this as a critical feature.
rldleblanc commentedon Dec 4, 2025
I have this same exact need. With <0.8 I was able to use global variables and atomics to track the count in the
.match(), but as of 0.8, I can no longer do that with.is_true()since all mocks are checked causing issues with the counting. Since I need to send back a 5XX error with a payload I have to run with two mocks and that's double counting the global variable in my case. Would love to see traction on this feature.FalkWoldmann commentedon Oct 20, 2026
Implemented in #108 with
up_to_n_times, will be in 0.9