-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
Multiple Mocks isn't working as expected #60
Comments
Thanks for creating this issue. There are two features in play here:
The solution is to change the first mock definition to not match if the let mock_first = server.mock(|when, then| {
when.method(GET)
.path("/v1/mediaItems")
.query_param("pageSize", "3")
.matches(|req| {
!req.query_params
.as_ref()
.unwrap()
.iter()
.any(|(k, _)| k.eq("pageToken"))
});
then.status(200)
.header("Content-Type", "application/json")
.json_body(json!({
"mediaItems": [],
"nextPageToken": "the_next_page"
}));
}); I consider using a matcher function a workaround though. It's planned to add dedicated matchers for the absence of values (e.g.,. Does this answer your question? |
Yes, thanks for the detailed explanation, as well as the helpful library you're sharing. Aside from this issue, it's otherwise been really easy to use, so thanks for sharing this great work. Having #44 could simplify the code, so look forward to it. Have you considered modifying how requests are matched with mocks when there are multiple? For example, in my case since the second request had an extra query parameter one could say it more closely matches Another way I've seen this done is with stateful behavior. So when the first request is made, the mock would transition to a new state and allow the second mock to be matched. Although that would be considerably more work and not sure as useful in this case considering the additional complexity. Anyways -- appreciate the work and slick library. Thanks again. |
Yes, I have considered matching requests to the "closest" mock and it was a conscious decision to leave it out. In fact, I have considered stateful behaviour and I think this functionality might be a nice addition to the library. However, time is the limiting factor here :-) |
I'm trying to use the library to test some code that uses a Google API. The endpoint will return a pagination token in each response until there are no more resources to fetch. I was trying to test some code to see if the pagination was working and came across this unexpected behavior. In my test, I have two mocks with the same path, but different query string parameters -- here is a simplified version.
I'd expect
mock_first
to match the first request andmock_last
to match the last since the query parameters specified in the match and in each request are different, but that doesn't appear to be the case. When I run the code I get this error:Do I have a misunderstanding of how multiple mocks can be used or is this a bug in how mocks match requests?
The text was updated successfully, but these errors were encountered: