久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何模擬 django 信號處理程序?

How do I mock a django signal handler?(如何模擬 django 信號處理程序?)
本文介紹了如何模擬 django 信號處理程序?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個通過裝飾器連接的 signal_handler,就像這個非常簡單的:

I have a signal_handler connected through a decorator, something like this very simple one:

@receiver(post_save, sender=User, 
          dispatch_uid='myfile.signal_handler_post_save_user')
def signal_handler_post_save_user(sender, *args, **kwargs):
   # do stuff

我想做的是用模擬庫 http://www.voidspace 模擬它.org.uk/python/mock/ 在測試中,檢查 django 調(diào)用它的次數(shù).我現(xiàn)在的代碼是這樣的:

What I want to do is to mock it with the mock library http://www.voidspace.org.uk/python/mock/ in a test, to check how many times django calls it. My code at the moment is something like:

def test_cache():
    with mock.patch('myapp.myfile.signal_handler_post_save_user') as mocked_handler:
        # do stuff that will call the post_save of User
    self.assert_equal(mocked_handler.call_count, 1)

這里的問題是即使模擬了原始信號處理程序也會被調(diào)用,很可能是因為 @receiver 裝飾器在某處存儲了信號處理程序的副本,所以我模擬了錯誤的代碼.

The problem here is that the original signal handler is called even if mocked, most likely because the @receiver decorator is storing a copy of the signal handler somewhere, so I'm mocking the wrong code.

所以問題是:我如何模擬我的信號處理程序以使我的測試工作?

So the question: how do I mock my signal handler to make my test work?

請注意,如果我將信號處理程序更改為:

Note that if I change my signal handler to:

def _support_function(*args, **kwargs):
    # do stuff

@receiver(post_save, sender=User, 
          dispatch_uid='myfile.signal_handler_post_save_user')
def signal_handler_post_save_user(sender, *args, **kwargs):
   _support_function(*args, **kwargs)

我改為模擬 _support_function,一切都按預(yù)期工作.

and I mock _support_function instead, everything works as expected.

推薦答案

所以,我最終得到了一種解決方案:模擬信號處理程序只是意味著將模擬本身連接到信號,所以這正是我做了:

So, I ended up with a kind-of solution: mocking a signal handler simply means to connect the mock itself to the signal, so this exactly is what I did:

def test_cache():
    with mock.patch('myapp.myfile.signal_handler_post_save_user', autospec=True) as mocked_handler:
        post_save.connect(mocked_handler, sender=User, dispatch_uid='test_cache_mocked_handler')
        # do stuff that will call the post_save of User
    self.assertEquals(mocked_handler.call_count, 1)  # standard django
    # self.assert_equal(mocked_handler.call_count, 1)  # when using django-nose

請注意,mock.patch 中的 autospec=True 是必需的,以使 post_save.connect 能夠在 上正確工作MagicMock,否則django會拋出一些異常,連接會失敗.

Notice that autospec=True in mock.patch is required in order to make post_save.connect to correctly work on a MagicMock, otherwise django will raise some exceptions and the connection will fail.

這篇關(guān)于如何模擬 django 信號處理程序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數(shù)點/精度)
Converting Float to Dollars and Cents(將浮點數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: h在线免费观看 | 99热激情 | 国家一级黄色片 | 欧美精品乱码久久久久久按摩 | 91亚洲免费 | 黄色三级在线播放 | h视频网站在线观看 | 在线观看免费av网 | 欧美成人aaa级毛片在线视频 | 久久精品久久久久久 | 国产免费一区二区三区网站免费 | 精品一区二区三区四区外站 | 国产精品视频免费观看 | 国产免费福利小视频 | 99久久久久国产精品免费 | 夜夜爽99久久国产综合精品女不卡 | 日本免费一区二区三区 | 国产欧美视频一区 | 欧美1区2区 | 黄色在线免费观看视频网站 | 日韩在线精品视频 | 久久久.com | 精品视频成人 | 春色av| 一区二区精品 | 亚洲精品亚洲人成人网 | 涩涩导航 | 欧美一区二区 | av 一区二区三区 | 一区二区三区欧美 | 中文字幕国产视频 | 99久久精品国产一区二区三区 | 免费视频一区二区 | 九九在线 | 特一级毛片 | 国产激情视频在线观看 | 亚洲欧美一区二区三区视频 | 日韩av在线一区二区三区 | h视频免费在线观看 | 狠狠干影院 | 狠狠操狠狠干 |