Mocking class by replacing it
NickName:Derte Trdelnik Ask DateTime:2020-10-12T21:26:50

Mocking class by replacing it

imagine you have a module

from module.my_module import Foo


def my_function():
    foo = Foo()
    return whatewer_can_happen(foo)

that you want to test like this

class TestFoo:
    def __init__():
         pass
    
    def dont_care():
        return 9


def test_something():
    with mock.patch('tested_module.Foo') as mocked_class:
        mocked_class.side_effect = lambda *args: TestFoo(*args)
        assert tested_module.my_function() == 'not important'

essentially as if you wanted to inject the TestFoo in place of Foo (example wont work ofc, only for illustration)

How to mock the Foo class in the tested module without changing the tested code? If it is not possible, why - how are classes in python so different to functions that mocking them is not possible?

I am not looking for ways to do it differently(factory function/mocking only class functions directly etc)

It would in some cases just be helpful (and more readable) to be able to write mock classes and inject them for use into the tested module

Copyright Notice:Content Author:「Derte Trdelnik」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64318831/mocking-class-by-replacing-it

More about “Mocking class by replacing it” related questions

Mocking class by replacing it

imagine you have a module from module.my_module import Foo def my_function(): foo = Foo() return whatewer_can_happen(foo) that you want to test like this class TestFoo: def __init__()...

Show Detail

Class mocking in Django tests

I'm using mock 1.0.1 (http://www.voidspace.org.uk/python/mock/) to mock objects in my tests, and I'm able to mock dates using this approach: def dateStub(): return timezone.make_aware(datetime...

Show Detail

Mocking ANY class use in Java (specifically in Android)

I am coming from the .NET world (C#) to Java (Android development). Many mocking frameworks and tools from the .NET space allow replacing/overriding class/method usage without injecting any mock o...

Show Detail

Warning when mocking a class

I am reading this article - https://www.dartlang.org/articles/mocking-with-dart/ - about mocking wiht Dart and have gotten this simple example working. import 'package:unittest/mock.dart'; clas...

Show Detail

internal class state when mocking with moq

I am new to mocking and i just started to use moq. The tool states that we can use moq for mocking classes without interfaces. I was wondering if it is posible when mocking to keep the internal sta...

Show Detail

Partial Class Mocking

I want to use hippomocks to mock a method in a class. That method is called by another method in the same class. As in... class Foo { public: Foo() {} virtual ~Foo() {} virtual string getNa...

Show Detail

Mocking a method in a class dynamically

I am pretty new to Mockito and Spring. I am trying to mock many methods of many classes. I want to create a functionality where bean name and method name can given as input as string, and it will t...

Show Detail

Issue mocking a class variable value in java

I have a class that has a private class variable initialized like public class MyClass{ private BusinessObject businessObject = BusinessObjectGenerator.getBusinessObject(); public MyClas...

Show Detail

Mocking complex return types

I am a newbie with powermock, mockito and facing an issue with complex return types. Example: Suppose the following method needs to be mocked: public someReturnType getMethod(param1, param2){ ....

Show Detail

AttributeError when patching class variable when mocking

I have this file structure mocking_module - sample_class.py - class_util.py - test_base_class_attribute_override.py And I defined a class like this. It has a class variable called bas...

Show Detail