I use python mocks to claim that a specific object was created with the correct arguments. This is what my code looks like:
class Installer:
def __init__(foo, bar, version):
pass
def __enter__(self):
return self
def __exit__(self, type, value, tb):
pass
def install(self):
pass
class Deployer:
def deploy(self):
with Installer('foo', 'bar', 1) as installer:
installer.install()
Now I want to claim that it installerwas created with the right arguments. This is the code that I still have:
class DeployerTest(unittest.TestCase):
@patch('Installer', autospec=True)
def testInstaller(self, mock_installer):
deployer = Deployer()
deployer.deploy()
mock_installer.__init__.assert_called_once_with('foo', 'bar', 1)
This is the error I get:
File "test_deployment.py", line .., in testInstaller
mock_installer.__init__.assert_called_once_with('foo', 'bar', 1)
AttributeError: 'function' object has no attribute 'assert_called_once_with'
Here is a fixed code (call it test.py). Thank you all!
import unittest
from mock import patch
class Installer:
def __init__(self, foo, bar, version):
pass
def __enter__(self):
return self
def __exit__(self, type, value, tb):
pass
def install(self):
pass
class Deployer:
def deploy(self):
with Installer('foo', 'bar', 1) as installer:
installer.install()
class DeployerTest(unittest.TestCase):
@patch('tests.test.Installer', autospec=True)
def testInstaller(self, mock_installer):
deployer = Deployer()
deployer.deploy()
mock_installer.assert_called_once_with('foo', 'bar', 1)