mercredi 6 mai 2015

Use arguments passed to function when using mock patch

I have a function that I'd like to test:

def get_doc(path, s3_bucket):
    """ Gets docs from s3 """
    with tempfile.TemporaryDirectory() as tmpdirname:
        k = Key(s3_bucket)
        k.key = path
        temp_path = os.path.join(tmpdirname, filename)
        k.get_contents_to_filename(temp_path)
        doc_file = open(temp_path, 'rb')
    return doc_file

I believe the part that I need to mock is get_contents_to_filename; however, in order to correctly mock this I need to place a file at the temp_path location. This is what I have so far:

def test_get_doc(self):
    with mock.patch.object(Key, 'get_contents_to_filename', return_value=None):
    doc = get_doc('/test/path')
    self.assertEqual(test, 'text etc.')

How can I access the temp_path variable with mock?

Aucun commentaire:

Enregistrer un commentaire