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?
You can replace
foo
with your own function that does the check:This way your check is done at the moment
foo
would have been called in the tested code, as needed.