OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

MagickMock() assert_called not recognizing calls

  • Thread starter Thread starter Ni3dzwi3dz
  • Start date Start date
N

Ni3dzwi3dz

Guest
i have a function like this, stored in file example.py:

Code:
def my_func(file):
    conn = get_connection()
    conn.upload_file(file)
    conn.execute_file(file)

Now, i want to test it, so i`m using MagicMock in test_example.py like:

Code:
@mock.patch("example.get_connection")
def test_my_func(mock_conn):
    mock_conn = MagickMock()
    mock_conn.upload_file = MagickMock(return_value=True)
    
    result= my_func("file.zip")
    mock_conn.upload_file.assert_called()

Now, the test fails with error:

Code:
        if self.call_count == 0:
            msg = ("Expected '%s' to have been called." %
                   (self._mock_name or 'mock'))
>           raise AssertionError(msg)
E           AssertionError: Expected 'upload_file' to have been called.

When i debug the test, in example.py, MagickMocks attribute called` is set to True after the call, but back in test_example.py it is False, and the test fails. What am i doing wrong?
<p>i have a function like this, stored in file example.py:</p>
<pre><code>def my_func(file):
conn = get_connection()
conn.upload_file(file)
conn.execute_file(file)

</code></pre>
<p>Now, i want to test it, so i`m using MagicMock in test_example.py like:</p>
<pre><code>@mock.patch("example.get_connection")
def test_my_func(mock_conn):
mock_conn = MagickMock()
mock_conn.upload_file = MagickMock(return_value=True)

result= my_func("file.zip")
mock_conn.upload_file.assert_called()
</code></pre>
<p>Now, the test fails with error:</p>
<pre><code> if self.call_count == 0:
msg = ("Expected '%s' to have been called." %
(self._mock_name or 'mock'))
> raise AssertionError(msg)
E AssertionError: Expected 'upload_file' to have been called.
</code></pre>
<p>When i debug the test, in example.py, MagickMock<code>s attribute </code>called` is set to True after the call, but back in test_example.py it is False, and the test fails. What am i doing wrong?</p>
 

Latest posts

Top