Fix for .returns object inequality - #79
Merged
Merged
Conversation
When a property / method is mocked with returns, and one of the arguments is an object (or a class or a function), the `areArgumentsEqual` function in utilities failed to verify. It executed the default behaviour `a === b`, which is never true for objects (only if a and b have the same object reference)
Owner
|
This was by design, and is also how NSubstitute (which this library is a port of) behaves. If you want to match arguments by value, can't you use |
Collaborator
Author
|
I think I wasn't clear enough, the issue appears when evaluating the argument of a mock defined with // In the unit test
const db = Substitute.for<IFetch>()
const data = IData.create()
db.getUpdates(Key.create()).returns(Promise.resolve(data))
const service = new Service(db)
await service.handle()
// In the Service class
.
.
.
const updateData = await this.getData(Key.create()) // returns context.proxy instead of dataWhen the class or function on the unit test calls db.getUpdates(Key.create()), I would expect to receive a Promise that resolves with data. The substitute.received method works as expected, and there is no issue with |
Owner
|
Ah I see - my bad! Nice fix! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #36
This fixes the issue of ticket 36. It didn't have anything to do with promises, but with how the arguments equality for a mock with .return was performed.
So if we mocked a method where one of the arguments was an object or class, unless the object had the same reference,
areArgumentsEqualfunction returned false.When this ocurred, the mocked method / property, instead of returning the expected value, it returned context.proxy, which had all sort of side effects, one of them being that sometimes an error was thrown with the message
Cannot convert object to primitive value.I made a little change on the proxies, they are based now on a dummy SubstituteJS class. I'm not sure yet if this will help with debugging when something goes wrong, but by doing that, and adding the getOwnPropertyDescriptor handler, instead of getting the arguments not matching error with the generic [[Function]], it will write [[SubstituteJS]]