I have a struct with a function that returns a String
from a json using serde_json::to_string_pretty()
. I am trying to write a unit test to verify the output of the string.
#[test]
fn test() {
...
let j =
json!({
"mock_key": "val",
"another_key": "another_val",
});
assert_eq!(serde_json::to_string_pretty(&j).unwrap(), json_string);
When I run the test, it passed for the first time. And if I try to run the test for the second time , the test fails because:
Diff < left / right > :
{
< "mock_key": "val",
< "another_key": "another_val"
> "another_key": "another_val",
> "mock_key": "val"
}
If I try to run it again, the test passes, and the cycle continues.
Why is that? And is there a better way to do it?
I tried changing the order in let j = json!({...})
.
The test should pass (or fail) each time I run it with no changes made to the code.
pigeon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.