Reputation: 113
I have a method that I would like to create a unittest for. ConfigObj is just a library for parsing config files. The method is very simple:
from configobj import ConfigObj
def read_name():
conf = ConfigObj('/data/myfile')
return conf['name']
conf['name'] returns a string value of the 'name' setting in the file. I am trying to write a unit test to mock this return behavior, but I am running into a "KeyError" on conf['name']. It doesn't look like the conf instance was mocked at all.
My test so far:
@mock.patch('configobj.ConfigObj')
def test_read_name(self, mock_configobj):
config = mock_configobj.return_value
config.__getitem__.side_effect = 'tom'
self.assertEqual(read_name(), 'tom')
Upvotes: 0
Views: 86
Reputation: 113
As it turns out it was very simple
@patch('configobj.ConfigObj.__getitem__', return_value='tom')
def test_get_name(self, mock_configobj)
self.assertEqual(read_name(), 'tom')
Upvotes: 1