def test__cleanup_failed(self, send_notification, post_deploy): """Cleanup fails if update_state raises""" f = FSM(state_id) f.ch = mock.Mock(pika.channel.Channel) f.conn = mock.Mock(pika.connection.Connection) f.failed = True # Testing the fail notification too with mock.patch.object(f, 'update_state', mock.Mock(side_effect=Exception("derp"))) as ( us_exception): with self.assertRaises(Exception): with mock.patch('recore.amqp.CONF') as notif_conf: notif_conf = NOTIFICATION_CONF f._cleanup() # At the very end a notification should go out no matter what self.assertEqual(send_notification.call_count, 1) assert send_notification.call_args[0][4] == 'failed' post_deploy.assert_called_once()