MarkBerlin78 / pymox

Automatically exported from code.google.com/p/pymox
Apache License 2.0
0 stars 0 forks source link

Mox should offer a stubout call which is safe to call repeatedly and won't try to mock out the mock object #10

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I've been using code like this to do it.  I'm happy to contribute a patch
if you agree this is useful and isn't dangerous for some reason that I'm
not seeing:

def StubOutIfNeeded(mox_obj, parent_object, attribute):
  """Stubs out the call only if it's not already a mock."""
  if not isinstance(getattr(parent_object, attribute),
                    (mox.MockAnything, mox.MockObject)):
    mox_obj.StubOutWithMock(parent_object, attribute)

Then if I want to set multiple expectations on the thing that I'm stubbing
out, I can safely call StubOutIfNeeded(self.mox, module, 'FunctionName')
each time before setting an expectation, and not worry about whether it's
already been stubbed out or not.

Combined with using mox.MoxTestBase which will unset all stubs in self.mox,
this seems pretty safe, because things will succeed whether I create and
use the stub or don't.

Original issue reported on code.google.com by br...@salomaki.com on 29 May 2009 at 5:30

GoogleCodeExporter commented 9 years ago
This is my first time using Google Code, so I don't know if I have a way to set
something as an Enhancement rather than a Defect, or to change priority, but I 
would
have at least called this an enhancement if I had the opportunity...

Original comment by br...@salomaki.com on 29 May 2009 at 5:31