How to mock.patch a class imported in another module
Question: I have a python class with such a module:

**xy.py**

    
    
    from a.b import ClassA
    
    class ClassB:
      def method_1():
        a = ClassA()
        a.method2()
    

then I have ClassA defined as:

**b.py**

    
    
    from c import ClassC
    
    class ClassA:
      def method2():
          c = ClassC()
          c.method3()
    

Now in this code, when writing test for xy.py I want to mock.patch ClassC, is
there a way to achieve that in python?

obviously I tried:

    
    
    mock.patch('a.b.ClassA.ClassC)
    

and

    
    
    mock.patch('a.b.c.ClassC')
    

None of these worked.


Answer: You need to patch where `ClassC` is located so that's `ClassC` in `b`:

    
    
    mock.patch('b.ClassC')
    

Or, in other words, `ClassC` is imported into module `b` and so that's the
scope in which `ClassC` needs to be patched.

