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

  • Flash CS6 Video Tutorials for Beginners (Actionscript 3 Gaming)

    Flash CS6 Vi

    14 EYLÜL 2012
  • Ricardo Cerqueira

    Ricardo Cerq

    28 Mayıs 2008
  • Vicious Computers

    Vicious Comp

    14 EKİM 2006