django:mock post_save信号处理程序?

时间:2016-11-07 13:28:14

标签: django mocking django-testing

tests.py

from unittest.mock import patch

from orders.models import Order

class OrderModelTest(CartSetupTestCase):

    def test_string_representation(self):
        # Mocking Order's post_save signal
        with patch('orders.signals.post_save_order', autospec=True) as mocked_handler:
            post_save.connect(
                mocked_handler,
                sender=Order,
                dispatch_uid='test_cache_mocked_handler'
            )
            order = Order.objects.create(
                user=self.user,
                merchant_uid="1475633246629",
                customer_name="asd",
                address="주소",
                address_detail="asdfdsa",
                postal_code="12345",
                phone_number="01095104344",
                possible_date_start="2011-11-24",
                possible_date_end="2011-11-24",
                possible_time_start="11:22 AM",
                possible_time_end="11:22 AM",
                total_price=self.cart.total_price,
            )

signals.py

@receiver(post_save, sender=Order, dispatch_uid="spacegraphy")
def post_save_order(sender, instance, created, **kwargs):
    if created:
        SlackNotification.objects.create(
            receiver="order_web",
            content="asdfasdf"
        )

我跟着https://stackoverflow.com/a/13119150/3595632,但它不起作用,这意味着,它实际上称为信号处理程序! (我使用print()

进行了检查

有什么不对吗?

1 个答案:

答案 0 :(得分:0)

FWIW我也遵循了同样的想法,但需要模仿芹菜 send_task 。经过阅读,我确实认识到,信号是有价值的,不应该被嘲笑(它是一个理想的行动来解雇它们,对吧?),所以解决办法是嘲笑里面发生了什么信号(与外部服务通信)。总而言之,我建议:

$ pip --version
pip 9.0.1 from /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)

$ pip install Django==1.11.7
Collecting Django==1.11.7
  Downloading Django-1.11.7-py2.py3-none-any.whl (6.9MB)
    100% |████████████████████████████████| 7.0MB 184kB/s 
Requirement already satisfied: pytz in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from Django==1.11.7)
Installing collected packages: Django
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 316, in clobber
    ensure_dir(destdir)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/utils/__init__.py", line 83, in ensure_dir
    os.makedirs(path)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/django'