I am trying to test a function using python mock.
Foo.py
session = scoped_session(sessionmaker(bind=db_state.engine))
@use_kwargs({
'user_id': fields.Integer(location='json', required=True)
})
def create_user(user_id):
with session as s:
try:
result = user_dao.create_user(s, user_id)
return json.dumps(result), 200, {'Content-Type': 'application/json; charset=utf-8'}
except SQLAlchemyError:
result = {"Fatal": "Id not found"}
return json.dumps(result), 404, {'Content-Type': 'application/json; charset=utf-8'}
The unitest for this method is :
test.py
class CreateUserTest(unittest.TestCase):
@patch("user_dao.create_user")
@patch("flask.request")
def test_create_user(self, mock_request, mock_create_user):
mock_create_user.side_effect = SQLAlchemyError
response, code, content = foo.create_user()
multiple assert statements go here.
The stacktrace is not much very helping here. May be I am missing the obvious. Here's it
Traceback (most recent call last):
File "/binlib/python2.7/site-packages/mock/mock.py", line 1305, in patched
return func(*args, **keywargs)
File "/some/path/test.py", line 6, in test_create_user
response, code, content = foo.create_user()
File "/bin/lib/python2.7/site-packages/webargs-1.3.4-py2.7.egg/webargs/core.py", line 434, in wrapper
return func(*args, **kwargs)
File "/some/path/foo.py", line 8, in create_user
result = user_dao.create_user(s, user_id)
File "/bin/lib/python2.7/site-packages/mock/mock.py", line 1062, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "/bin/lib/python2.7/site-packages/mock/mock.py", line 1118, in _mock_call
raise effect
TypeError: __init__() takes at least 4 arguments (1 given)
I did check the code for the mock.py and cannot spot what is going wrong here.