Python 如何断言已使用预期数据调用该方法?

Python 如何断言已使用预期数据调用该方法?,python,unit-testing,mocking,pytest,Python,Unit Testing,Mocking,Pytest,我有以下代码: class-CreateProfile: 定义初始化(自我、配置文件存储库): self.profile\u repository=profile\u repository 定义调用(自我、创建配置文件请求): 如果self.profile\u repository.exists\u by\u user\u id(创建\u profile\u request.user\u id): 提升配置文件ReadyExists(创建配置文件请求。用户id) profile=profile(

我有以下代码:

class-CreateProfile:
定义初始化(自我、配置文件存储库):
self.profile\u repository=profile\u repository
定义调用(自我、创建配置文件请求):
如果self.profile\u repository.exists\u by\u user\u id(创建\u profile\u request.user\u id):
提升配置文件ReadyExists(创建配置文件请求。用户id)
profile=profile(创建\u profile\u request.user\u id,无,无,无)
self.profile\u repository.create(profile)
使用以下各项进行测试:

@补丁(“profile.core.service.create_profile.profile”)
def test_应_创建_空_配置文件(
配置文件,配置文件存储库,创建配置文件请求,服务
):
profile=ProfileFactory(用户\u id=sentinel.profile\u id)
Profile.return\u值=Profile
服务(创建配置文件请求)
profile_repository.create.assert_调用_with(profile)
断言sentinel.profile_id==profile.id
assert None是profile.name
断言None是profile.nick
assert None是profile.bio
assert None是profile.status
问题是,如果a对代码进行以下更改,测试仍然是绿色的

class-CreateProfile:
定义初始化(自我、配置文件存储库):
self.profile\u repository=profile\u repository
定义调用(自我、创建配置文件请求):
如果self.profile\u repository.exists\u by\u user\u id(创建\u profile\u request.user\u id):
提升配置文件ReadyExists(创建配置文件请求。用户id)
profile=profile(创建\u profile\u request.user\u id,无,无,无)
id=profile.id
profile.id=None
self.profile\u repository.create(profile)
profile=id
虽然这个更改做起来有点愚蠢,因此不可能发生,但我觉得在测试用例中遗漏了一些东西,因为我无法确定发送到存储库的数据在调用时是否正确。我曾经写过一段代码,它将存储参数的副本,并使方法mock
side_effect
执行它,然后检查已存储的数据,但这似乎是有人已经做过的事情,因为我没有发现任何像这样做的事情,所以我陷入了以下问题:


我应该测试这个吗?如果是的话,我如何才能正确执行呢?

我建议您尝试模拟您正在尝试执行的呼叫,并利用该功能

我强烈建议您熟悉补丁函数和模拟作为一个整体