ts-mockito
ts-mockito copied to clipboard
deepEqual() matcher should match against value at call time
When verifying a call to a mocked function with a mutable parameter, I use a strict equality matcher if I care about the identify of the object, and a deepEqual
matcher if I care about the object's state at call time.
But when using the deepEqual
matcher, it matches against the current state of the mutable object - even if it has changed since call time.
import 'mocha';
import { deepEqual, instance, mock, verify } from 'ts-mockito';
it('matches the parameter at call time', () => {
class Foo {
bar(value: Readonly<any>): void {}
}
let mockFoo: Foo = mock(Foo);
let array: number[] = [0];
instance(mockFoo).bar(array); // call to be verified: bar([0])
array.push(1); // modify the parameter after the call
verify(mockFoo.bar(deepEqual([0]))).once(); // assertion fails but should pass
verify(mockFoo.bar(deepEqual([0, 1]))).once(); // assertion passes but should fail
});
Hi @taltened, you are right. Verification is processed by using instance, not the state when real call has been made.
Thanks for reporting this. I think that something like deep clone
on every call should fix this issue. I will add this to roadmap.
Hello. Any updates on this issue?