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
I'm writing some custom matchers using the TypeMpq.It.is() method referencing a const object in the passed predicate. The test fails on the === check. See the following code:
import * as TypeMoq from 'typemoq';
class FooBar {
public static foo(someMethod: SomeMethod, bar: Bar): void {
someMethod({ bar: bar });
}
}
type SomeMethod = (payload: any) => void;
class Bar {
public _name: string;
constructor(name: string) {
this._name = name;
}
}
describe('FooBar example', () => {
const BAR = new Bar('Bob');
const someMethodMock: TypeMoq.IMock<SomeMethod> = TypeMoq.Mock.ofType<SomeMethod>();
it('test', () => {
FooBar.foo(someMethodMock.object, BAR);
someMethodMock.verify(
someMethod => someMethod(TypeMoq.It.is((payload: { bar: Bar }) => {
return payload.bar === BAR;
})), TypeMoq.Times.once());
});
});
As JS/TS pass by reference objects, I was expecting this test to pass. Unfortunately, the === fails. Objects are obviously equals but, at this point, they are not the same instance (memory addresses are different) thus failing the test.
Is this an issue, an implementation details/specificity or an incomprehension of myself around JS/TS with arrow functions and pass by reference?
Ok I think I understand why the first case (without the setup but with the TypeMoq.It.is call in the verify) was failing.
In your implementation, I'm guessing you are saving an invocation to a method by keeping a deep copy of parameters. Doing otherwise would result in weird issues since you have no guaranty that code under test will not modify these objects/arrays (there's no issue for primitives).
So if I understand correctly, the right way to verify in my test scenario is to use the 3rd case/example (verify without TypeMoq.It.is). There's no way to do a === verify on objects/arrays due to language limitation (objects/arrays are passed by reference forcing your library to do a deep copy on passed parameters while recording invocations).
Sorry for disturbing. Looks like I finally figure it out myself 😂
I'm writing some custom matchers using the
TypeMpq.It.is()
method referencing a const object in the passed predicate. The test fails on the===
check. See the following code:As JS/TS pass by reference objects, I was expecting this test to pass. Unfortunately, the
===
fails. Objects are obviously equals but, at this point, they are not the same instance (memory addresses are different) thus failing the test.Is this an issue, an implementation details/specificity or an incomprehension of myself around JS/TS with arrow functions and pass by reference?
Here's the test output results:
Thank you
The text was updated successfully, but these errors were encountered: