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

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.