1

在编写 Python 时,我是一个完全的新手,更不用说测试它了。

这是我的 Flask 端点:

@blueprint.route('/mailing_finish/<account_id>/<sumall_stream_id>/', methods=['POST'])
def mailing_finish(account_id, sumall_stream_id):
    """
    Get Response for mailing_id and update Dataset:
    * MYEMMA_EMAIL_SENDS: response['sent']
    """

    # TODO: webhook does not fire

    data = json.loads(request.data)['data']

    access_token = sumall_redis.get_oauth_token(account_id)

    response_data = sumall_audience.get_response_data(
        access_token=access_token,
        account_id=account_id,
        mailing_id=data['mailing_id'],
    )

    event = {
        'timeStamp': data['timestamp'][3:],
        'eventId': 'mailing_id:{mailing_id}'.format(
            mailing_id=data['mailing_id'],
        ),
        'data': {
            'MYEMMA_EMAIL_SENDS': {
                'value': response_data['recipient_count'],
                'dimensions': [
                    {
                        'dimension': 'MAILINGS',
                        'value': data['mailing_id'],
                    },
                ],
            },
        },
    }

    status = sumall_api.post_stream_event(
        stream_id=sumall_stream_id,
        event=event,
    )

    return 'Data sent to SumAll', status

这是我的测试:

@mock.patch('sumall.utils.sumall_api')
@mock.patch('sumall.utils.sumall_audience')
@mock.patch('sumall.utils.sumall_redis')
def test_mailing_finish(self, sumall_redis_mock, sumall_audience_mock, sumall_api_mock):

    sumall_redis_mock.get_oauth_token.return_value = self.access_token

    sumall_audience_mock.get_response_data.return_value = {'recipient_count': 2}

    event = {
        'timeStamp': self.data['timestamp'][3:],
        'eventId': 'mailing_id:{mailing_id}'.format(mailing_id=self.data['mailing_id'],),
        'data': {
            'EMAIL_SENDS': {
                'value': sumall_audience_mock.get_response_data.get('recipient_count'),
                'dimensions': [
                    {
                        'dimension': 'MAILINGS',
                        'value': self.data['mailing_id'],
                    },
                ],
            },
        },
    }

    res = self.client.post(
        '/webhooks/mailing_finish/{account_id}/{sumall_stream_id}'.format(
            account_id=self.account_id,
            sumall_stream_id=self.sumall_stream_id
        ),
        data=json.dumps({'data': self.data}),
        content_type='application/json',
    )

    sumall_redis_mock.get_oauth_token.assert_called_with(self.account_id)

    sumall_audience_mock.get_response_data.assert_called_with(
        access_token=self.access_token,
        account_id=self.account_id,
        mailing_id=self.data['mailing_id']
    )

    sumall_api_mock.post_stream_event.assert_called_with(
        stream_id=self.sumall_stream_id,
        event=event,
    )

我收到的错误是:

AssertionError: Expected call: get_oauth_token('123456')
Not called

我不确定出了什么问题。任何帮助将不胜感激!谢谢!

4

1 回答 1

4

sumall_redis您在视图模块中作为本地名称导入,但模拟原始sumall.utils.sumall_redis.

您可能在视图模块的顶部有这个:

from sumall.utils import sumall_redis

这将该对象绑定到模块中的本地名称。当测试开始并应用补丁时,只有模块sumall_redis中的原始对象sumall.utils会受到影响,而不是这个本地名称。

您需要模拟视图模块中绑定的名称:

@mock.patch('view_module.sumall_redis')

这也适用于您的其他 2 个进口商品。

mock文档包括您可能想要阅读的补丁指南。

于 2014-02-14T13:31:55.447 回答