So I'm trying to make fun of all the stripe web hooks in a method so that I can write a Unit test for it. I use the mock library to bully stripe methods. Here is the method I'm trying to make fun of:
class AddCardView(APIView): """ * Add card for the customer """ permission_classes = ( CustomerPermission, ) def post(self, request, format=None): name = request.DATA.get('name', None) cvc = request.DATA.get('cvc', None) number = request.DATA.get('number', None) expiry = request.DATA.get('expiry', None) expiry_month, expiry_year = expiry.split("/") customer_obj = request.user.contact.business.customer customer = stripe.Customer.retrieve(customer_obj.stripe_id) try: card = customer.sources.create( source={ "object": "card", "number": number, "exp_month": expiry_month, "exp_year": expiry_year, "cvc": cvc, "name": name } )
This is the method for unit testing :
@mock.patch('stripe.Customer.retrieve') @mock.patch('stripe.Customer.create') def test_add_card(self,create_mock,retrieve_mock): response = { 'default_card': None, 'cards': { "count": 0, "data": [] } }
Now stripe.Customer.retrieve ridiculed properly. But I can not scoff at customer.sources.create . I am really stuck with this.
source share