ts-mockito icon indicating copy to clipboard operation
ts-mockito copied to clipboard

deepEqual() matcher should match against value at call time

Open taltened opened this issue 6 years ago • 2 comments

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
});

taltened avatar Jul 07 '18 23:07 taltened

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.

NagRock avatar Aug 23 '18 10:08 NagRock

Hello. Any updates on this issue?

zoryamba-work avatar Apr 11 '23 18:04 zoryamba-work