lundi 31 août 2015

How can I mock/patch an associative array in python

I have a module with a dictionary as associative array to implement a kind-of switch statement.

def my_method1():
    return "method 1"


def my_method2():
    return "method 2"


map_func = {
    '0': my_method1,
    '1': my_method2
}

def disptach(arg):
  return map_func[arg]()

How can I mock my_method1 in tests? I've tried the following without success:

import my_module as app

@patch('my_module.my_method1')
def test_mocking_sample(self, my_mock):
    my_mock.return_value = 'mocked'
    assert_equal('mocked',app.dispatch('0'))

Any idea?

Aucun commentaire:

Enregistrer un commentaire