How to mock a class in Python?
Question: I'm new to Python, but I used to work with ruby.

I'm trying to do some tests with Python but I can't figure out how I mock a
class for my test.

Let's say I have this real class:

    
    
    from database import DB
    class Foo:
      def do_it(self):
        x = {'key': 'value'}
        DB.save(x)
        # do other stuff with x
        return x
    

Now, I want to test if everything I'm doing with x returns me the right
result, and I want to test if DB.save was called with x as parameter, but I
don't want to actually save x to database. To see if DB.save is being called,
I can use some mock framework, but how can I tell Foo class that I need it to
use the mock object and not the real DB object?

Thank You.

**EDIT:** Sorry, my example wasn't enough for what I need to accomplish.

Let's see this new example:

    
    
    from database import DB
    class Foo:
      DB.connect()
      def do_it(self):
        x = {'key': 'value'}
        DB.save(x)
        # do other stuff with x
        return x
    

And for instance, let's say the database is down.

When I import Foo, it raises the connection error and I do not have time to
mock DB.

What should I do??


Answer: The answer is, don't write your classes like that. It's a strange way to do so
anyway - there shouldn't really be any executable code at the class level,
other than attribute and method definitions. You should setup the connection
in the `__init__` method.

    
    
    class Foo(object):
        def __init__(self, *args, **kwargs):
            DB.connect()
    

