久久久久久久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 的意思不同)
主站蜘蛛池模板: 亚洲国产网 | 欧美日韩综合一区 | 久久久久久久国产 | 色爽女 | 99精品免费久久久久久久久日本 | 91精品国产综合久久精品 | 日韩高清在线 | www.久 | 国产一区影院 | 国产精品亚洲第一区在线暖暖韩国 | 天天色天天射天天干 | 国产日韩一区二区三区 | 91精品国产综合久久久久久丝袜 | 精品免费视频 | 日韩精品久久一区 | 日韩成人性视频 | 先锋资源在线 | 欧美精品一区二区在线观看 | 中文字幕日韩av | www.亚洲视频.com | 伊人久久精品一区二区三区 | 亚洲成人动漫在线观看 | 亚洲精品性视频 | 成人日韩 | 久久美国| 国产福利91精品一区二区三区 | 91久久国产精品 | 国产成人免费视频网站高清观看视频 | 久久九九99 | 久久不卡日韩美女 | 久久精品在线免费视频 | jvid精品资源在线观看 | 日韩久久精品 | 欧美综合久久 | 亚洲欧美在线一区 | 久久久这里只有17精品 | 日韩日b视频 | 日韩电影中文字幕 | 欧美日韩国产精品一区 | 国产91在线 | 中日 | 国产一级片久久久 |