mardi 3 mars 2020

Why mock when you can replace an object?

I'm trying to understand why I need to use mocks when I can just replace object I want to mock directly.

For example in this contrived example

class Child:
    def get(self):
        return {0 : "SomeDictResult"}

class Root:
    def __init__(self):
        self.child = Child()

    def get_info(self):
        return self.child.get()

def test_get_info(root : Root):
    root.child.get = lambda : {1 : "SomeOtherResult"} # This is where a mock would be

    print(root.get_info())

Instead of mocking, I am replacing get with an lambda function with a custom output. Why would I need to mock if I could replace the get function

Aucun commentaire:

Enregistrer un commentaire