Stumped on how to mock a class and be able to assert that its' methods are called with some arguments. When I assert the call i get a "Not called" assertion but, I can see the method call in the mock_calls
attribute.
sandbox/module.py
class Subject(object):
def __init__(self):
pass
def run(self, *args, **kwargs):
reference = Reference(*args, **kwargs)
reference.method_a(*args)
class Reference(object):
def __init__(self, *args, **kwargs):
pass
def method_a(self, *args):
pass
test.py
import unittest
from unittest import mock
from sandbox.module import Subject
class TestSandbox(unittest.TestCase):
@mock.patch('sandbox.module.Reference')
def test_method_calls(self, mock_reference):
subject = Subject()
subject.run(1, 2, 3, x=44, y=55, z=66)
mock_reference.assert_called_with(1, 2, 3, x=44, y=55, z=66)
mock_reference.method_a.assert_called_with(1, 2, 3)
The result is
AssertionError: Expected call: method_a(1, 2, 3)
Not called
The value of mock_reference.mock_calls
is
[
call(1, 2, 3, x=44, y=55, z=66),
call().method_a(1, 2, 3)
]
If I access call as call().method_a
I can access the method details but, the mock_calls
gets a item call()
added to it. Which could change assert_called_once_with
in a way i wouldn't expect and, doesn't feel quite right. Furthermore, if autospec=True
is used I need to pass the params again. Using just call
as in mock_reference.call.method_a
does not work either.
Output of mock_calls
when accessing call().method_a.mock_calls
mock_reference().method_a.mock_calls
[
call(1, 2, 3, x=44, y=55, z=66),
call().method_a(1, 2, 3),
call()
]
mock_reference.assert_called_once_with(1, 2, 3)
AssertionError: Expected 'Reference' to be called once. Called 2 times.
Mocks and stubs are fake Java classes that replace these external dependencies. These fake classes are then instructed before the test starts to behave as you expect. More specifically: A stub is a fake class that comes with preprogrammed return values.
What is mock testing? Mocking means creating a fake version of an external or internal service that can stand in for the real one, helping your tests run more quickly and more reliably. When your implementation interacts with an object's properties, rather than its function or behavior, a mock can be used.
Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.
The definition of mock is something fake, or something arranged for practice. A knock-off of a designer purse is an example of a mock purse. A trial that is practice for the real trial is an example of a mock trial.
You mocked a class, yet the second method is called on instance. Change
mock_reference.method_a.assert_called_with(1, 2, 3)
to
mock_reference.return_value.method_a.assert_called_with(1, 2, 3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With