Voglio capire come @patch
una funzione da un modulo importato.
Questo è dove sono finora.
app/mocking.py:
from app.my_module import get_user_name def test_method(): return get_user_name() if __name__ == "__main__": print "Programma iniziale... " test_method()
app/my_module/__init__.py:
def get_user_name(): return "Utente non beffato"
test/mock-test.py:
import unittest da app.mocking import test_method def mock_get_user(): return "Mocked This Silly" @patch("app.my_module.get_user_name") class MockingTestTestCase(unittest.TestCase): def test_mock_stubs(self, mock_method): mock_method.return_value = "Mocked This Silly") ret = test_method() self.assertEqual (ret, "Mocked This Silly") if __name__ == "__main__": unittest.main()
Questo non funziona come mi aspetterei. Il modulo "rattoppato" restituisce semplicemente il valore non simulato di get_user_name
. Come faccio a deridere i metodi da altri pacchetti che sto importando in uno spazio dei nomi in prova?