verify() equality tests

26 views
Skip to first unread message

James Cooper

unread,
Jul 6, 2010, 1:28:37 PM7/6/10
to mockito-python
Hi,

I have a class that defines: __eq__(self, other)

I have stubbed it to always return True

I call it with something like:

verify(world.repository).insertContactHistory(contactHistory)

But it always fails with:

Wanted but not invoked: insertContactHistory(<entity.ContactHistory
instance at 0x10078cdd0>)

Is mockito using a normal == test, or is there something more
complicated going on?

I have modified my test to use a string. example:

verify(world.repository).insertContactHistory("foo")

And that works fine if I change the class under test to pass in
"foo". So strings are working, but my class is not.

thoughts?

-- James

szczepiq

unread,
Jul 7, 2010, 1:15:50 AM7/7/10
to mockito...@googlegroups.com
Hey,

Mockito should call normal ==. Not sure how is it now but Serhiy will probably know. In meantime you can look at the sources and find out yourself :) If it is a bug please do report it.

Without looking at the code I vaguely remember that perhaps you cannot stub __eq__ due to the fact it used internally (however I might be talking nonsense - don't remember :).

Cheers!
Szczepan


--
You received this message because you are subscribed to the Google Groups "mockito-python" group.
To post to this group, send email to mockito...@googlegroups.com.
To unsubscribe from this group, send email to mockito-pytho...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito-python?hl=en.


Serhiy Oplakanets

unread,
Jul 7, 2010, 4:05:59 AM7/7/10
to mockito-python
Hi James.
Wasn't really able to reproduce your issue. Could you answer a couple
questions:
1. Which version of Mockito are you using?
2. "> I have stubbed it to always return True" How did you do that? I
believe Mockito can't stub magic methods.
3. Do you have the source code available anywhere? Would be of really
much help. Or, can you try to reproduce it on some synthetic example?

And yes. It uses simple '==' comparison.

James Cooper

unread,
Jul 8, 2010, 9:47:35 AM7/8/10
to mockito-python
Hi everyone. Thanks for the responses

Re 1: mockito 0.4.0
Re 2: I misspoke. What I should have said is that I overrode __eq__
in my entity class and hardcoded "return True" in it
Re 3: Yes, I downloaded the source code and I think I figured out my
issue

It looks like mockito does a != test. I'm somewhat new to python and
just learned that python has __eq__ and __ne__ methods on a class.

When I implemented __ne__ like this:

def __ne__(self, other):
return not self.__eq__(other)

then mockito worked as expected.

So the short explanation is that you need to make sure your classes
implement __eq__ and __ne__ when using them as verify matchers in your
tests. It seems like good practice to override these methods in most
cases.

As an aside, I found this page and will probably write a base mixin
class to encapsulate these equality overrides. Including it here in
case anyone else runs into this issue:

http://stackoverflow.com/questions/390250/elegant-ways-to-support-equivalence-equality-in-python-classes

thank you

-- James
Reply all
Reply to author
Forward
0 new messages