python - how mock only one method called within the object you are testing -



python - how mock only one method called within the object you are testing -

i want test method mock out other methods calls. created simple illustration should illustrate concept:

class myclass(): def one_method(self): print "hey" def two_deep(self): self.one_method() def three_deep(self): self.two_deep()

i using python mock framework called mox , wrote next code this:

def test_partial(self): self_mox = mox.mox() some_object = myclass() ## 1. create mock my_mock = mox.mockobject(some_object) my_mock.one_method().andreturn('some_value') self_mox.replayall() ret = my_mock.three_deep() ## *** see note below called "comment": self_mox.verifyall()

comment:

i thought if called mock on method hadn't been overwritten, mock default original code, chain of calls want, lastly phone call beingness replaced... doesn't this. can't figure out how embed mock object within test object doesn't have inserting method.

i looked partial mocks , chained mocks solve this, couldn't find way pull off.

thanks help :)

-- peter

check documentation stuboutwithmock: https://code.google.com/p/pymox/wiki/moxdocumentation#stub_out https://code.google.com/p/pymox/wiki/moxrecipes#mock_a_method_in_the_class_under_test.

so needed is:

def test_partial(self): self_mox = mox.mox() # create class is, instead of doing mock. some_object = myclass() # stub particular method using stuboutwithmock. m.stuboutwithmock(some_object, "one_method") some_object.one_method().andreturn('some_value') self_mox.replayall() ret = some_object.three_deep() self_mox.unsetstubs() self_mox.verifyall()

python mocking mox partial-mocks

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -