Here is a generic code example that I am trying to test with mock. I get the AttributeError attribute.
Here is __init __. py:
import logging
log = logging.getLogger(__name__)
class SomeError(Exception):
pass
class Stuff(object):
def method_a(self):
try:
stuff = self.method_b()
except SomeError, e:
log.error(e)
return 'no ponies'
return 'omg ponies'
def method_b(self):
return ''
In tests.py, I have something like this:
import mock
import unittest
from package.errors import SomeError
from mypackage import Stuff
some_error_mock = mock.Mock()
some_error_mock.side_effect = SomeError
class MyTest(unittest.TestCase):
@mock.patch.object('Stuff', 'method_b', some_error_mock)
def test_some_error(self):
mystuff = Stuff()
a = mystuff.method_a()
self.assertTrue(a == 'no ponies')
When you run the test, mock raises an AttributeError, saying: "The material has no method_b attribute"
What am I doing wrong here?
source
share