0

So I am working with the below basic function:

def whatever(..): 
    for input_file in files:
        compressed_file = Path(input_file)
    if not compressed_file.name.replace('test_', '').startswith(COMPRESSED_FILE_PREFIX):
        continue

So I mock the Path function like this:

@mock.patch('path_to_Path_function.Path')
def test_choose_MFII_files(mock_Path):
    mock_Path.return_value = 'files/exchange' 
    mock_Path.name.return_value = 'whatever'

Now when I run this, I get an error saying

AttributeError: 'str' object has no attribute 'name'

I can see why I get this error, because mock_Path.return_value is set to a string! But how can I set mockPath.name.return_value to whatever; I just want to mock it out.

I think using MagicMock() somehow would work but I'm not sure how. Any help would be great!

ifrj
  • 91
  • 7
  • Your function returns `None` at the moment so there is nothing to mock in the return. – JonSG Jul 15 '23 at 21:12
  • You are testing code that expects the return value to have a `.name` attribute; strings don't have such an attribute; therefore, you cannot mock the return value with a string. Instead, mock it with an object that has the needed attribute; since you want that attribute to be a string, set it to a string. This is about understanding the logic behind the test conditions, not about how to use the mock framework. – Karl Knechtel Jul 15 '23 at 21:57

1 Answers1

1

You want:

@mock.patch('path_to_Path_function.Path')
def test_choose_MFII_files(mock_Path):
    mock_Path.return_value.name = 'whatever'
    path_to_Path_function.whatever(...)

mock_Path's return value (which defaults to another mock object) corresponds to compressed_file in the function under test, so the thing you want to set to a string is the name attribute of that object.

If you wanted to write it a little more verbosely to make the nesting more obvious it might look like:

@mock.patch('path_to_Path_function.Path')
def test_choose_MFII_files(mock_path_cls):
    mock_path_obj = mock_path_cls()
    mock_path_obj.name = 'whatever'
    path_to_Path_function.whatever(...) 
Samwise
  • 68,105
  • 3
  • 30
  • 44