【问题标题】:Mocking simple-salesforce模拟 simple-salesforce
【发布时间】:2019-10-11 19:57:12
【问题描述】:

我是在 Python 中模拟 API 调用的新手,找不到任何文档来说明如何模拟对使用请求的 API 的调用的任何简单示例。

特别是,我需要模拟对 simple-salesforce 的调用。我尝试过使用 Reponses 和 requests-mock,但我总是在 simples-salesforce 库中遇到一些异常。

谁能帮我简单模拟一下这个电话?如果我能让一个人工作,我想我可能会从那里过得很好。这是我的登录电话:

self.salesforce = Salesforce(
        username=username,
        password=password,
        security_token=token,
        instance_url=self.instance_url,
        domain=self.sandbox,
        proxies=proxies,
    )
    return result

任何帮助将不胜感激!

【问题讨论】:

    标签: python-3.x unit-testing mocking simple-salesforce


    【解决方案1】:

    您需要做的是patch Salesforce 符号正在使用它(您要测试的模块)

    您需要确保Saledforce 符号本身是一个模拟。您还需要确保在调用此模拟时,您返回另一个模拟对象。这是因为您正在调用 Salesforce() 方法并且应该返回一个 Salesforce 对象。

    这可能看起来像这样:

    @mock.patch('full.path.to.your.test.module.Salesforce')
    def test_sample(mock_salesforce_constructor):
        mock_salesforce = MagicMock()
        mock_salesforce_constructor.return_value = mock_salesforce
    
        # set up mock call return values on `mock_salesforce`
    
        # call your under-test method
    
        # assert & verify that the mock was called propely
    

    您可以在此处查看有关设置模拟对象的更多信息:https://docs.python.org/3/library/unittest.mock.html

    【讨论】:

      猜你喜欢
      • 2021-07-09
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多