def StubOutClassWithMocks(self, obj, attr_name):
    """Replace a class with a "mock factory" that will create mock objects.

    This is useful if the code-under-test directly instantiates
    dependencies.  Previously some boilder plate was necessary to
    create a mock that would act as a factory.  Using
    StubOutClassWithMocks, once you've stubbed out the class you may
    use the stubbed class as you would any other mock created by mox:
    during the record phase, new mock instances will be created, and
    during replay, the recorded mocks will be returned.

    In replay mode

    # Example using StubOutWithMock (the old, clunky way):

    mock1 = mox.CreateMock(my_import.FooClass)
    mock2 = mox.CreateMock(my_import.FooClass)
    foo_factory = mox.StubOutWithMock(my_import, 'FooClass',
                                      use_mock_anything=True)
    foo_factory(1, 2).AndReturn(mock1)
    foo_factory(9, 10).AndReturn(mock2)
    mox.ReplayAll()

    my_import.FooClass(1, 2)   # Returns mock1 again.
    my_import.FooClass(9, 10)  # Returns mock2 again.
    mox.VerifyAll()

    # Example using StubOutClassWithMocks:

    mox.StubOutClassWithMocks(my_import, 'FooClass')
    mock1 = my_import.FooClass(1, 2)   # Returns a new mock of FooClass
    mock2 = my_import.FooClass(9, 10)  # Returns another mock instance
    mox.ReplayAll()

    my_import.FooClass(1, 2)   # Returns mock1 again.
    my_import.FooClass(9, 10)  # Returns mock2 again.
    mox.VerifyAll()
    """
    attr_to_replace = getattr(obj, attr_name)
    attr_type = type(attr_to_replace)

    if attr_type == MockAnything or attr_type == MockObject:
      raise TypeError('Cannot mock a MockAnything! Did you remember to '
                      'call UnsetStubs in your previous test?')

    if attr_type not in self._USE_MOCK_FACTORY:
      raise TypeError('Given attr is not a Class.  Use StubOutWithMock.')

    factory = _MockObjectFactory(attr_to_replace, self)
    self._mock_objects.append(factory)
    self.stubs.Set(obj, attr_name, factory)
dataset/ETHPy150Open AppScale/appscale/AppServer/lib/mox/mox.py/Mox.StubOutClassWithMocks/original