Refactor proposal for test asserts#172
Conversation
|
I think that using the |
Hey, yes. It was only a fast example to support my main question. If you think it worths refactoring the asserts I will create a new issue. It's something not so important, I think. |
|
@da2ce7 has used an alternative solution here: pub async fn assert_ok(response: Response) {
let response_status = response.status().clone();
let response_headers = response.headers().get("content-type").cloned().unwrap();
let response_text = response.text().await.unwrap();
let details = format!(
r#"
status: ´{response_status}´
headers: ´{response_headers:?}´
text: ´"{response_text}"´"#
);
assert_eq!(response_status, 200, "details:{details}.");
assert_eq!(response_headers, "application/json", "\ndetails:{details}.");
assert_eq!(response_text, "{\"status\":\"ok\"}", "\ndetails:{details}.");
}With this formatted string, we can eliminate the need for a new struct for this grouped data. pub struct ResponseDetails {
pub status: String,
pub headers: String,
pub text: String,
}but when the higher-level assert fails, I would like to show the original line where it fails, not one of these internal asserts. The good thing about this solution is we show the whole context regardless of which assertion failed. |
|
Moving this to a discussion: #356 |
hi @da2ce7 @WarmBeer, I'm not happy with the asserts I've written so far for the integration tests.
For example. Considering this test:
when it fails, it gives you a not-very-good message.
When the response body is wrong:
When the HTTP status is wrong:
The problem is it gives you the line number in the assert.
I think there are two solutions:
This PR is a proof of concept for the second solution. Of course, we could use the standard
Fromtrait to instantiate this new struct from the response instead of the explicit constructor.This new implementations shows this output:
What do you think?