samedi 2 mai 2020

Why mock doesn't patch passed class?

I want to test if class extending tkinter.Tk is initialized with correct parameters. Imports work correctly. I can use both main and Gui, but I don't understand why do I get AssertionError: Expected 'Gui' to be called once. Called 0 times. with mocked. This statement mocker.patch('bookmeister.gui.Gui') seems to be correct for me if from bookmeister.gui import Gui works.

from bookmeister.__main__ import main
from bookmeister.gui import Gui

def test_run(mocker):
    mocker.patch.object(Gui, 'mainloop')
    mocked = mocker.patch('bookmeister.gui.Gui')
    spy = mocker.spy(Gui, '__init__')
    main()
    mocked.assert_called_once_with('Bookstore Manager', '600x470')
    spy.assert_called_once_with('Bookstore Manager', '600x470')

I have also tried to use mocker.spy, but then another error is displayed.

AssertionError: Expected call: __init__('Bookstore Manager', '600x470') Actual call: __init__(<bookmeister.gui.Gui object .>, 'Bookstore Manager', '600x470')

I have no idea how I can simulate <bookmeister.gui.Gui object .> in my test.

Could you please explain me how I can correct my code to test right initialization?

Aucun commentaire:

Enregistrer un commentaire