仅使用装饰器配置 return 值模拟

Configure return value mock with decorator only

有没有办法在 patch 装饰器中捕获以下逻辑,而不必将模拟传递给函数:

@patch('boto3.client')
def test_playing_with_saml(self, boto3_client):
    boto3_client.return_value.assume_role_with_saml = lambda *args, **kwargs: ('foo', 'bar')
    self.assertEqual(playing_with_saml(), 'expected')

不,不是真的,不是没有指定 boto3_client 的其余部分,不会 会更清晰或更具可读性。

我不会在这里使用 lambda,我会设置模拟的 return 值:

boto3_client.return_value.assume_role_with_saml.return_value = ('foo', 'bar')

现在您可以对 boto3_client.return_value.assume_role_with_saml 方法进行断言(就像断言它已被调用)。