Reputation: 143
I have the following code:
class CreateProfile:
def __init__(self, profile_repository):
self.profile_repository = profile_repository
def __call__(self, create_profile_request):
if self.profile_repository.exists_by_user_id(create_profile_request.user_id):
raise ProfileAlreadyExists(create_profile_request.user_id)
profile = Profile(create_profile_request.user_id, None, None, None, None)
self.profile_repository.create(profile)
tested with the following:
@patch("profile.core.service.create_profile.Profile")
def test_should_create_empty_profile(
Profile, profile_repository, create_profile_request, service
):
profile = ProfileFactory(user_id=sentinel.profile_id)
Profile.return_value = profile
service(create_profile_request)
profile_repository.create.assert_called_with(profile)
assert sentinel.profile_id == profile.id
assert None is profile.name
assert None is profile.nick
assert None is profile.bio
assert None is profile.status
the problem is that if a make the following change to the code, the test will still be green.
class CreateProfile:
def __init__(self, profile_repository):
self.profile_repository = profile_repository
def __call__(self, create_profile_request):
if self.profile_repository.exists_by_user_id(create_profile_request.user_id):
raise ProfileAlreadyExists(create_profile_request.user_id)
profile = Profile(create_profile_request.user_id, None, None, None, None)
id = profile.id
profile.id = None
self.profile_repository.create(profile)
profile = id
Although this change is kinda dumb to be done and therefore not expected to happen, I feel like I am missing something in the test case because I cannot be sure that the data that I sent to the repository is correct at the moment it has been called. I once wrote a piece of code that would store a copy of the arguments and made method mock side_effect
to execute it and later check the data that has been stored but it seems like something that someone would already have done and as I did not found anything beeing done like this I am stuck with the following question:
Should I be testing this? And if yes, how can I do it correctly?
Upvotes: 0
Views: 298
Reputation: 201
I would recommend looking into mocking the call that you are trying to do and levering the assert_called_with functionality.
I would highly recommend this video for getting familiar with the patch function and mocks as a whole.
Upvotes: 2