2016-10-21 14 views
0

私はユニットテストに関数をしようとしています:単体テストでboto3 sns呼び出しをモックできますか?

@shared_task() 
def push_notification(message=None, message_type=None, user_id=None, data={}): 

    # Get the aws arn from token table 
    aws_token_data = AwsDeviceToken.objects.filter(user_id=user_id).latest("id") 
    client = boto3.client('sns', **aws.AWS_CREDENTIAL) 

    message = { 
     'default': message, 
     more stuff here 
     'data': data}) 
    } 
    message = json.dumps(message, ensure_ascii=False) 
    response = client.publish(
     TargetArn=str(aws_token_data.aws_PLATFORM_endpoint_arn), 
     Message=message, 
     MessageStructure='json', 
     MessageAttributes={} 
    ) 
    return response 

ユーザーは、当社のサービスに登録すると、彼らは、デバイスタイプに基づいてトピックARNを取得します。私が試してみました

self.userは、テストケースのセットアップ方法に登録されたユーザである
def test_push_notification(self): 
    with mock.patch('boto3.client') as mock_client: 
     data = {'Some data': "to be sent"} 
     push_notification(
      message="your invitation has been accepted", 
      message_type='b2g_accepted', 
      user=self.user, 
      data=data 
     ) 
     self.assertEqual(mock_client.call_count, 1) 

。これは失敗し、call_countは0です

私はこの機能をテストする方法を見つけようとしていますが、主にS3のサードパーティのモジュールやサンプルを思いついています。

(それがクラスメソッドである場合を除き)すべてのヘルプは

答えて

1

を高く評価しているあなたは、それがインポートされる場所をモックする必要が

だからあなたpush_notification機能がmy_moduleと呼ばれるモジュールである場合、次のように記述する必要があります

With mock.patch('my_module.boto3.client') as mock_client 
関連する問題