仅使用装饰器配置返回值模拟

时间:2017-07-03 09:20:20

标签: python mocking python-mock magicmock

有没有办法在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')

1 个答案:

答案 0 :(得分:1)

不,不是真的,不是没有指出boto3_client的其余部分,会更清晰或更易读。

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

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

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