[TIP] Using CaptureMock for calls that return objects

Geoff Bache geoff.bache at gmail.com
Wed Apr 6 23:47:36 PDT 2011


Hi Kumar,

We've done loads of this kind of stuff with CaptureMock, and it
definitely supports objects... see for example the "email" example at
http://texttest.carmen.se/index.php?page=capturemock&n=python_basic
for what it's supposed to look like.

But it all depends on what you're intercepting and where the things
that get returned are defined. You need to make sure that CaptureMock
knows it's supposed to intercept the returned objects as well. I think
you've identified a docs problem because I don't think it makes this
very clear.

For example, a common gotcha when intercepting "urllib2" is that the
objects returned by the urlopen method are in fact defined in
"urlllib". So if you just tell it to intercept "urllib2" you'll get a
problem similar to what you describe below. The trick is to tell it to
intercept both, and then it will work.

Failing that, please post some more details...

Regards,
Geoff

On Thu, Apr 7, 2011 at 12:37 AM, Kumar McMillan
<kumar.mcmillan at gmail.com> wrote:
> Hi Geoff and all,
> I've been curious to see if CaptureMock [1] can speed up my test
> suite's database access by recording the calls after fixture data has
> loaded then replaying the calls using the captured results.  The issue
> I'm running into is that CaptureMock does not seem to handle calls
> that return objects (as opposed to native types).  Before I post some
> tracebacks has someone already done something similar to this with
> CaptureMock?
>
> To give a specific example, I am trying to capture/replay a Django
> manager, something like MyModel.objects.all().  The return value of
> all() is an iterable of MyModel() instances that I'd like to get
> replayed from a cached file.  They don't have to be actual instances,
> just things that can be used like object.name, object.foo.
>
> [1] http://texttest.carmen.se/index.php?page=capturemock
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>



More information about the testing-in-python mailing list