I am trying to write a unit test for a class that looks like below.
import boto
class ToBeTested:
def location(self, eb):
return eb.create_storage_location()
def some_method(self):
eb = boto.beanstalk.connect_to_region(region, access_key, secret_key)
location(eb)
Is there a way to mock boto.beanstalk.connect_to_region return value and finally mocking create_storage_location? I am new to patch and mock in python, so I have no idea how could I go about doing that. Could someone please let me know if there is a way to do this?
Thanks much!
The idea is to patch connect_to_region() so that it returns a Mock object, then you can define whatever methods you want on the mock, example:
import unittest
from mock import patch, Mock
class MyTestCase(unittest.TestCase):
@patch('boto.beanstalk.connect_to_region')
def test_boto(self, connect_mock):
eb = Mock()
eb.create_storage_location.return_value = 'test'
connect_mock.return_value = eb
to_be_tested = ToBeTested()
# assertions
See also:
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With