I have a function in python that creates an isntance of a Project
class. If there is no data available, a default instance is created. The code in python is similar to this:
def create_project(data):
try:
if data:
return module.Project(data)
else:
return DefaultProject()
except:
raise Exception("Can't create project.')
I want to test, if the constructor of the Project
class is called with the right parameter. I already test, if the constructor is called with the following test:
@mock.patch('module')
def test_create_to_return_created_project(self, module_mock):
#arrange
project = mock.MagicMock()
module_mock.Project = mock.MagicMock(return_value = project)
#act
result = module.create_project('some_data')
#assert
self.assertEqual(result, project)
Now I want to test, if the constructor of the Project
class gets the right parameter value with the following code:
@mock.patch('module')
def test_create_project_with_right_parameter(self, module_mock):
#arrange
mockProject = mock.MagicMock()
module_mock.Project = mockProject
#act
module.create_project('some_data')
#assert
mockProject.__init__.assert_called('some_data')
But the code results in the error:
Message: 'function' object has no attribute 'assert_called'
Does anybody know how to test, if the constructor of the Project
class is called with the right parameter?