'We patch runtime.publish to capture all XBlock events sent during
the test.
This is a little bit ugly -- it\'s all dynamic -- so we patch
__init__ for the system runtime to capture the
dynamically-created publish, and catch whatever is being
passed into it.'

def setUp(self):

    super(XBlockEventTestMixin, self).setUp()
    saved_init = lms.djangoapps.lms_xblock.runtime.LmsModuleSystem.__init__
    def patched_init(runtime_self, **kwargs):
        '\n                                                Swap    out    publish    in    the    __init__\n                                                '
        old_publish = kwargs['publish']
        def publish(block, event_type, event):
            '\n                                                                Log    the    event,    and    call    the    original    publish\n                                                                '
            self.events.append({'event': event, 'event_type': event_type})
            old_publish(block, event_type, event)
        kwargs['publish'] = publish
        return saved_init(runtime_self, **kwargs)
    self.events = []
    lms_sys = 'lms.djangoapps.lms_xblock.runtime.LmsModuleSystem.__init__'
    patcher = mock.patch(lms_sys, patched_init)
    patcher.start()
    self.addCleanup(patcher.stop)
