0

I want to check if a local function (declared on the test itself) is called.

For example:

def test_handle_action():
    action = "test"
    user = "uid"
    room = "room"
    content = "test"
    data = {}

    def test_this(user, room, content, data):
        pass

    handlers = {action: test_this}

    with mock.patch("handlers.handlers", handlers):
        with mock.patch(".test_this") as f:
            handle_action(action, user, room, content, data)

            f.assert_called_with()

How can I mock path the function test_this inside my test?

With .test_this I got the error:

E       ValueError: Empty module name
Rodrigo
  • 135
  • 4
  • 45
  • 107

1 Answers1

2

If test_this is a mocked function, you can define test_this as a Mock object and define assertions upon it:

from unittest import mock

def test_handle_action():
    # GIVEN
    action = "test"
    user = "uid"
    room = "room"
    content = "test"
    data = {}

    test_this = mock.Mock()

    handlers = {action: test_this}

    with mock.patch("handlers.handlers", handlers):
        # WHEN
        handle_action(action, user, room, content, data)

        # THEN
        test_this.assert_called_with(user, room, content, data)
Anson Miu
  • 1,171
  • 7
  • 6