<div dir="ltr"><span style="font-size:12.8000001907349px">Hi!</span><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">I've just started a new project using Twisted and I want to write unit tests since the beginning. Unfortunately I've got some trouble understanding how should I do it. I read 'Test-driven development with Twisted', read some articles on the web and searched on the mailing list but I couldn't find anything which make it clear for me.<br><br>I've got a class:<br><br><div>class SessionCleaner(object):</div><div>    def __init__(self, session_db, interval=10):</div><div>        self.session_db = session_db</div><div>        <a href="http://self.lc/" target="_blank">self.lc</a> = task.LoopingCall(self.check_old_sessions)</div><div>        self.lc.start(interval)</div><div><br></div><div>    @defer.inlineCallbacks</div><div>    def check_old_sessions(self):</div><div>        log.msg('check_old_sessions()', logLevel=logging.DEBUG)</div><div>        try:</div><div>            old_sessions = yield self.session_db.get_old_sessions()</div><div>            for s in old_sessions:<br></div><div>                yield self.session_db.process_stopped(s)</div><div><br></div><div>        except txredisapi.ConnectionError as e:</div><div>            log.msg('check_old_sessions - connection error {}'</div><div>                    .format(e), logLevel=logging.WARNING)</div></div><div style="font-size:12.8000001907349px"><br>session_db is a object with methods which makes some calls to Redis.<br><br>Testing if __init__ works correctly is easy - I can mock task.LoopingCall and check if it was called with correct attributes.<br><br>I've got trouble testing check_old_sessions. Since I'm writing unit tests I don't want to call real session_db methods and make real Redis queries. I'd like to mock them and test just few things:<br>- is the method get_old_sessions called?<br>- is the method process_stopped called N times with the arguments returned by mocked get_old_sessions?<br>- is txredisapi.ConnectionError handled correctly?<br><br>So is there any "right" way of mocking functions which returns deferreds? Or maybe I should test this method differently?<br><br>Best regards,</div><div style="font-size:12.8000001907349px">Patryk</div></div>