Author: mattchung

  • Mocking boto exception

    I was getting so frustrated.

    I knew how to raise the exception with side_effect=. But, how do you mock the exception?

    try:
        connection = connect_to_sqs
    except BotoServerError as m:
        if m.error_code == "AlreadyExistsException"
    

    To get it to work, I inherited the exception class – BotoServerError

    from boto.exceptions import BotoServerError
    
    class MockBotoServerError(BotoServerError):
        def __init__(self, error_code)
            self.error_code = error_code
    
    @mock.patch('cloudwhale.build.connect_to_sqs', side_effect=MockBotoServerError())
    

    Order of decorator

    Also, pay attention to the order of parameters. My assertions were failing left and right.