mercredi 2 septembre 2015

Having difficulty mocking object returned by patched function

I have an instance method on a Django form class that returns a Python object from a payment service if successful.

The object has an id property, which I then persist on a Django model instance.

I'm having some difficulty getting the mocked object to return its .id property correctly.

# tests.py

class DonationFunctionalTest(TestCase):
    with mock.patch('donations.views.CreditCardForm') as MockCCForm:
        MockCCForm.charge_customer.return_value = Mock(id='abc123')

        # The test makes a post request to the view here.

        # The view being tested calls:

        # charge = credit_card_form.charge_customer()
        # donation.charge_id = charge.id
        # donation.save()

However:

print donation.charge_id

# returns
u"<MagicMock name='CreditCardForm().charge_customer().id'

I expected to see "abc123" for the donation.charge_id, but instead I see a unicode representation of the MagicMock. What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire