Unit Test for twisted addCallback with nested method

121 Views Asked by At

I have a situation to write a unit test for the below code, please help me out here.

def method1(self):
    self.method2()
def method2(self):
        def method2_success(result):
            if result.code == 200:
                self.state = 'running'
            else:
                self.state = 'failed'
                self.reactor.callLater(10, self.method2)

        def method2_failed(failure):
            self.state = 'failed'
            self.reactor.callLater(10, self.method2)

        self.state = 'in_progress'
        d = self.method3()
        d.addCallbacks(method2_success, method2_failed)
        return d
def method3(self):
    return some_deffered_object

If anyone knows how can I write a unit test using twisted for it?

1

There are 1 best solutions below

0
On

I'm not familiar with twisted, but you should just follow the pattern as in the package itself, like class TCPConnectionTestsBuilder in src/twisted/internet/test/test_tcp.py.