How can I mock exists()
for certain paths only while making it do the real thing for any other path?
For example the class under test will call exists()
and would fail on the paths that were supplied to it, because they do not exist on the system where the tests are running.
With Mox one could completely stub out exists()
, but this would make the test fail because calls not related to the class under test will not act in the real way.
I guess I could use WithSideEffects()
to call my own function when exists()
is being called branching the call in two directions, but how can I access the original exists()
?
This is what I have so far:
def test_with_os_path_exists_partially_mocked(self):
self.mox.StubOutWithMock(os.path, 'exists')
def exists(path):
if not re.match("^/test-path.*$", path):
return call_original_exists_somehow(path)
else:
# /test-path should always exist
return True
os.path.exists(mox.Regex("^.*$")).MultipleTimes().WithSideEffects(exists)
self.mox.ReplayAll()
under_test.run()
self.mox.VerifyAll()