Reputation: 507
One of my classes needs to do something like this:
class A:
def __init__(self, someDict, someOtherObject):
self._someDict = someDict
self._someOtherObject = someOtherObject
def func(self):
self._someDict["key"] = None
self._someOtherObject.foo()
Now, I would like to check if "key"
exists inside the passed dictionary at the moment, foo was called.
class Test( unittest.TestCase ):
def test_funcShouldAddKeyToDictionaryBeforeCallingFoo(self):
objectMock = Mock()
someDict = {}
aUnderTest = A(someDict, objectMock)
aUnderTest.func()
# assert "key" in someDict when called objectMock.foo()
How can I check if the dictionary already has an entry before foo was called?
Upvotes: 2
Views: 953
Reputation: 16815
You can replace foo
with your own function that does the check:
class Test(unittest.TestCase):
def test_funcShouldAddKeyToDictionaryBeforeCallingFoo(self):
def check_dict():
self.assertIn("key", someDict)
objectMock = Mock()
someDict = {}
aUnderTest = A(someDict, objectMock)
objectMock.foo = check_dict # "check_dict" will be called instead of "foo"
aUnderTest.func()
This way your check is done at the moment foo
would have been called in the tested code, as needed.
Upvotes: 1