Django Viewflow如何模拟Viewflow处理程序

huangapple go评论61阅读模式
英文:

django viewflow how to mock viewflow handlers

问题

Say I have a flow class app1/flows.py

class MyFlow(Flow):
    start = ...
    do_stuff = flow.Handler(this.do_stuff_handler).Next(...)
    end = ...

    def do_stuff_handler(self, activation):
        ...

If I want to mock do_stuff_handler to assert if it has been called

class MyFlowTest(TestCase):

    @mock.patch('app1.flows.MyFlow.do_stuff_handler')
    def test_do_stuff_handler_called(self, mock_do_stuff_handler):
        ...

It appears the do_stuff_handler did not get patched. I did notice the flow class gets instantiated when starting up Django see here. I am struggling to find the correct path to patch the handler method. Any ideas?

英文:

Say I have a flow class app1/flows.py

class MyFlow(Flow):
    start = ...
    do_stuff = flow.Handler(this.do_stuff_handler).Next(...)
    end = ...

    def do_stuff_handler(self, activation):
        ...

If I want to mock do_stuff_handler to assert if it has been called

class MyFlowTest(TestCase):

    @mock.patch('app1.flows.MyFlow.do_stuff_handler')
    def test_do_stuff_handler_called(self, mock_do_stuff_handler):
        ...

It appears the do_stuff_handler did not get patched. I did notice the flow class gets instantiated when starting up Django see here I am struggling to find the correct path to patch the handler method. Any ideas?

答案1

得分: 1

this-references are resolved at a class import time, and kept inside flow.Handler member variable.

So I think mocking the app1.flows.MyFlow.instance.do_stuff.handler could helps.

英文:

this-references are resolved at a class import time, and kept inside flow.Handler member variable.

So I think mocking the app1.flows.MyFlow.instance.do_stuff.handler could helps

huangapple
  • 本文由 发表于 2023年5月15日 07:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定