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

  • gsipek

    gsipek

    20 Temmuz 2007
  • HDstarcraft

    HDstarcraft

    12 Mayıs 2009
  • TROPFEST

    TROPFEST

    27 Mart 2007