I have a function process_payment
that I want to do exception testing for:
def process_payment(event, context):
try:
payDictionary= json.loads(event["body"])
if "payment_method_id" in payDictionary:
intent = stripe.PaymentIntent.create(
payment_method = payDictionary['payment_method_id'],
amount = 1000,
currency = 'usd',
payment_method_types=["card"]
)
print(intent.status)
if intent.status == 'succeeded':
return {
"statusCode": 200,
"body": json.dumps({
'message': 'Payment succeeded',
}),
}
except Exception as e:
return {
"body": json.dumps({
"message": 'Payment failed. '+ str(e),
}),
}
I want to do exception testing for the above function so I wrote the following piece of code using the unittests framework for testing:
import unittest
from unittest import mock
from unittest.mock import patch, Mock
import json
import stripe
from stripe.util import convert_to_stripe_object
from . import app
def test_process_payment_exception(self):
event = {
'httpMethod': 'POST',
'body': '{"payment_method_id":"pm_1HGTb2GPqNNATumTCzrTXZ9e"}'
}
response = {}
with mock.patch('stripe.PaymentIntent.create', side_effect= Exception) as mock_process_payment:
stripe_obj = convert_to_stripe_object(response)
mock_process_payment.return_value= stripe_obj
self.assertRaises(Exception, app.process_payment, event, "")
This test code produces the following response:
======================================================================
FAIL: test_process_payment_exception (hello_world.test_app.TestStudent)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\...\sam-app\hello_world\test_app.py", line 524, in test_process_payment_exception
app.process_payment(event, "")
AssertionError: Exception not raised
----------------------------------------------------------------------
Ran 2 tests in 0.024s
FAILED (failures=1)
I am struggling to figure out how to do exception testing for this function without making additional changes to my original code.
EDIT:
I changed my code to look like this:
def test_process_payment_exception(self):
event = {
'httpMethod': 'POST',
'body': '{"payment_method_id":"pm_1HGTb2GPqNNATumTCzrTXZ9e"}'
}
def mock_method():
raise Exception("someMessage")
with mock.patch('stripe.PaymentIntent.create') as mock_process_payment:
stripe_obj = convert_to_stripe_object(response)
mock_process_payment.side_effect= mock_method
ret= app.process_payment(event, "")
print(ret)
getmessage= json.loads(ret['body'])
getmessageFinal= getmessage["message"]
self.assertEqual("someMessage", getmessageFinal)
This then produced the following response:
Traceback (most recent call last):
File "C:\Users\...\sam-app\hello_world\test_app.py", line 536, in test_process_payment_exception
....
......
+ Payment failed. mock_method() got an unexpected keyword argument 'payment_method'
-------------------- >> begin captured stdout << ---------------------
Payment failed. mock_method() got an unexpected keyword argument 'payment_method'
--------------------- >> end captured stdout << ----------------------
----------------------------------------------------------------------
Ran 2 tests in 0.024s
FAILED (failures=1)
I don't understand why I am seeing mock_method() got an unexpected keyword argument 'payment_method'
.