I mocked the following function os.getenv, but instead of getting the return_value I specified, the Mock Object itself is returned. What am I doing wrong here?
@staticmethod
def setup_api_key():
load_dotenv() # loading apikey and secret into PATH Variables
api = os.getenv('APIKEY')
secret = os.getenv('SECRET')
return api, secret
The test looks like this:
def test_setup_api_key(self):
with patch('os.getenv') as mocked_getenv:
mocked_getenv = Mock()
mocked_getenv.return_value = '2222'
result = Configuration.setup_api_key()
self.assertEqual(('2222', '3333'), result)
setup_api_keyis defined in modulefoo, you need to mockfoo.os.getenv, notos.getenv.foo.osandosare two different names for the same module, and patching is just a process of redefining names.