Creating a mock without calling the real class constructor

110 views
Skip to first unread message

Marcin Zajączkowski

unread,
Feb 24, 2012, 8:30:08 AM2/24/12
to pydoubles
Hi,

I'm working with some legacy code and would like to make a stub/mock
of a collaborator which does some logic in a constructor (which is
also hard to mock itself). Construction like:
collaborator = stub(Collaborator())
or better
collaborator = stub(Collaborator(someObject))
calls the real constructor which is something I would like to avoid.
Using empty_stub() has also its drawbacks.

Is it possible to create a mock without calling the real class
constructor?

Regards
Marcin

Marcin Zajączkowski

unread,
Feb 24, 2012, 8:50:06 AM2/24/12
to pydoubles
Sending a question is very often enough to get an idea a few seconds
after :).
collaborator = stub(Collaborator) seems to work fine.

Regards
Marcin

Carlos Ble

unread,
Feb 24, 2012, 10:25:00 AM2/24/12
to pydo...@googlegroups.com
Hehe, yeah, that works but I have to document it :-)

Let me know if you find any issues with this behavior.

Cheers

2012/2/24 Marcin Zajączkowski <msz...@wp.pl>

David Villa

unread,
Feb 27, 2012, 2:28:28 AM2/27/12
to pydo...@googlegroups.com
Mmm, I would have to check, but I fear that "Collaborator" is taken as
the object instance, i.e. this seems a double for the Collaborator
class (the type instance). Constructor is never called because nobody
creates a instance at any time.

2012/2/24 Carlos Ble <ble.j...@gmail.com>:

David Villa Alises

unread,
Sep 5, 2012, 3:35:42 PM9/5/12
to pydo...@googlegroups.com
Hi programmers:

doublex allows to create "verified" doubles from *instances or classes* (at your election). Doublex internally never creates instances from your collaborator classes. The only exception to this is ProxySpy that, for obvious reasons, requires an instance. The following doubles are all valid:

Stub(Collaborator) or Stub(Collaborator())
Spy(Collaborator) or Spy(Collaborator())
ProxySpy(Collaborator())
Mock(Collaborator) or Mock(Collaborator())

And their "mimic" counterparts:

Mimic(Stub, Collaborator) or Mimic(Stub, Collaborator())
[...]

Cheers
Reply all
Reply to author
Forward
0 new messages