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
mockito stores mock's parameters to allow us to check them via captureAny. But the instance's fields may have changed between the mock call and the verify :
final user =User(name:"foo");
...
// Mistake, we called remote service (with a serialization) before changing the name field
apiClient.patchUser(user);
user.name ="bar";
// Testing with a mock apiClientfinal user =verify(apiClient.patchUser(captureAny)).single.captured asUser;
expect(user.name, "bar") // Passed, false-positive
I think it makes sense to store a cloned object to make sure it doesn't change between the interaction and the verification. Of course I'm not sure about the side-effect.
The text was updated successfully, but these errors were encountered:
Interesting case. There is no way to clone an Object of unknown type. We could store the hashCode, in addition to the actual argument which was passed, and then, as you say, do a quick verification step on its way out in .captured.single... This would catch a subset of cases that are like your description, as long as the .name is an input to the hashCode.
Anyway, I think a good practice is to avoid as much as possible mutables objects and always create new instance when possible.
That's what I did after discovering that.
The hashcode check could be an acceptable compromise
mockito stores mock's parameters to allow us to check them via
captureAny
. But the instance's fields may have changed between the mock call and theverify
:I think it makes sense to store a cloned object to make sure it doesn't change between the interaction and the verification. Of course I'm not sure about the side-effect.
The text was updated successfully, but these errors were encountered: