0
我有一個Django窗體類的實例方法,如果成功,它將從支付服務返回一個Python對象。難以模擬修補功能返回的對象
該對象有一個id
屬性,然後我堅持Django模型實例。
我有一些困難,讓嘲笑的對象返回其.id
屬性正確。
# tests.py
class DonationFunctionalTest(TestCase):
def test_foo(self):
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()
但是:
print donation.charge_id
# returns
u"<MagicMock name='CreditCardForm().charge_customer().id'
我期望看到 「ABC123」 爲donation.charge_id
,而是我看到了MagicMock
的Unicode表示。我究竟做錯了什麼?