title: Python Mock a Function Parameter
body: <p>I have a function in <code>myfile.py</code> that accepts no arguments. Inside the function I am creating a pandas <code>DataFrame</code> based on the data returned from an API call shown as: <code>df = pd.DataFrame(api_call_from_helper_function().all_the_data())</code>. I am trying to mock the API call so it doesn't try to actually connect to the API. Below is my attempt:</p>
  
  <p><strong>myfile.py</strong></p>
  
  <pre><code>def make_dataframe_from_data():
      df = pd.DataFrame(api_call_from_helper_function().all_the_data())
      return df
  </code></pre>
  
  <p><strong>test_myfile.py</strong></p>
  
  <pre><code>import pandas as pd
  from unittest.mock import MagicMock
  import myfile
  from myfile import make_dataframe_from_data
  
  
  def test_make_dataframe_from_data(monkeypatch):
      mock_df = MagicMock()
      mock_instance = MagicMock(return_value=mock_df)
      monkeypatch.setattr("pd.DataFrame", mock_instance)
      result = make_dataframe_from_data()
      mock_instance.assert_called_once()
      assert isinstance(result, pd.DataFrame())
  </code></pre>
  
  <p>In this code, the <code>api_call_from_helper_function()</code> uses another function I made to make the api call and the appended <code>.all_the_data()</code> returns all the data that it got. Right now my tests can mock the api call in <code>api_call_from_helper_function()</code> but I can't get it to mock when I pass it as a parameter to <code>pd.DataFrame</code>. What am I doing wrong?</p>