Creating a matcher for equals-method #167

Closed
Phoscur opened this Issue Dec 29, 2011 · 2 comments

Comments

Projects
None yet
3 participants
@Phoscur

Phoscur commented Dec 29, 2011

I don't know if you do this, but I build my ValueObjects with an equals-method. So I can define my own conditions to the equality of two objects. These are to be compared by

obj1.equals(obj2)

As the standard matcher from jasmine doesn't check for this method, I wrote my own matcher:

toBeEqualTo: function(expected) {
    return this.acutal.equals(expected);
}

TypeError: Cannot call method 'equals' of undefined

Really? I was quite sure about the rest of the code, so I reversed it:

toBeEqualTo: function(expected) {
    return expected.equals(this.acutal);
}

Error: Expected { method : Function, equals : Function, ... } to be equal to { method : Function, equals : Function, ... } at ... bla

Huh? So now I'm confused. Are these objects undefined or equal but not equal?

Also, my objects have a toString/toJSON method, I like those to be used if I see them printed out.

@ragaskar

This comment has been minimized.

Show comment Hide comment
@ragaskar

ragaskar Feb 14, 2012

Contributor

How are you calling your toBeEqualTo method? It sounds like there's some things not getting defined here.

You do realize you have a typo for actual? This may be what is causing your problem, if this code is directly copy-pasted from your implementation. We set this.actual, not this.acutal.

Contributor

ragaskar commented Feb 14, 2012

How are you calling your toBeEqualTo method? It sounds like there's some things not getting defined here.

You do realize you have a typo for actual? This may be what is causing your problem, if this code is directly copy-pasted from your implementation. We set this.actual, not this.acutal.

@infews

This comment has been minimized.

Show comment Hide comment
@infews

infews May 27, 2013

Contributor

Also note that Jasmine allows for custom equality testers. This is very poorly (read: NOT) documented for 1.x. But for 2.0 we're making it more explicit.

Look at the matchers_redo branch to see how you can add your own equality tester to the equals work. This will then get used everywhere that equality is tested (e.g., toContain, toHaveBeenCalledWith).

Closing.

Contributor

infews commented May 27, 2013

Also note that Jasmine allows for custom equality testers. This is very poorly (read: NOT) documented for 1.x. But for 2.0 we're making it more explicit.

Look at the matchers_redo branch to see how you can add your own equality tester to the equals work. This will then get used everywhere that equality is tested (e.g., toContain, toHaveBeenCalledWith).

Closing.

@infews infews closed this May 27, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment