SORU
17 AĞUSTOS 2009, PAZARTESİ


Nasıl bir açık bir ifadede kullanılan (Python Sahte çerçeve kullanarak) alay ediyor muyum?

Nasıl alay (alay, patch dekoratör ve nöbetçiler Michael Foord's Mock framework tarafından sağlanan kullanarak) ile: aşağıdaki kodu test edebilirim

def testme(filepath):
    with open(filepath, 'r') as f:
        return f.read()

CEVAP
24 Mayıs 2011, Salı


Bunu yapmanın yolu nihayet python protokolü yöntemleri (sihirli yöntemler), özellikle MagicMock kullanarak alay destekleyen: alay 0.7.0 değişti

http://www.voidspace.org.uk/python/mock/magicmock.html

İçerik yöneticisi olarak açık alaycı bir örnek (sahte belgelere örnekler sayfasından):

>>> open_name = '%s.open' % __name__
>>> with patch(open_name, create=True) as mock_open:
...     mock_open.return_value = MagicMock(spec=file)
...
...     with open('/some/path', 'w') as f:
...         f.write('something')
...
<mock.Mock object at 0x...>
>>> file_handle = mock_open.return_value.__enter__.return_value
>>> file_handle.write.assert_called_with('something')

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Joseph Hayhoe

    Joseph Hayho

    20 Mayıs 2010
  • KarnasCamillo

    KarnasCamill

    24 EKİM 2007
  • Tom Megalis

    Tom Megalis

    18 NİSAN 2006