-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When with literal object argument not working #209
Comments
TLDR: You can use when(something.method(deepEqual({ ... })).thenResolve(true) Without knowing the internals of ts-mockito this is likely due to the fact that object equality is checked by reference in JS. Being able to make sure a method is called with or returns an exact instance of an object is useful. For example (perhaps a bit contrived) consider an implementation of the identity function. You'd most likely not want the following implementation to pass. const identity = x => ({...x}) Otherwise you'd end up with weird behavior like const obj = { prop: 5 }
identity(obj) === obj // returns false The following test would fail with this implementation of const obj = { prop: 5 }
expect(identity(obj)).to.equal(obj) // fails That being said in most situations like the one you mention, you probably don't care whether the object passed as argument matches exactly by reference. The actual behavior we want in most cases is probably closer to ...
when(mock.modifyAndReturnTrueOnSucess(deepEqual(obj)).thenReturn(true))
... but your client looks like this (it calls ...
thing.modifyAndReturnTrueOnSucess(copy(obj))
...
// at this point the code may assume obj was modified even though it was not - a copy of the object was Your test may still pass even though |
Thanks @EmilEriksen! Would be great to have |
Thanks guys, Can you merge the PR please so i can close this one as well |
Whenever i call "when" with primitive type argument it gets work without any problems and the function return the desired value inside the test.
But when calling "when" with argument of reference type variables like literal object or a Map the function doesn't get mocked and it doesn't return the desired value from when.
is there any way to get "when" mocking works with literal objects arguments like above!
The text was updated successfully, but these errors were encountered: