1)
import mox
class MyRequestHandler(object):
def authenticate(self, request):
print "===in authenticate==="
handler = MyRequestHandler()
#####1) Start recording (put mock in record mode) #####
m = mox.Mox()
m.StubOutWithMock(handler, "authenticate")#stubout the method 'authenticate' and replace with a mock object named 'authenticate'
#####2) Stop recording (put mock objects in reply mode) #####
m.ReplayAll()
#####3) Verify that we played all recorded things #####
m.VerifyAll()
#####4) UnsetStubs #####
m.UnsetStubs()
2)
import mox
class MyRequestHandler(object):
def handle_request(self, request):
self.authenticate(request)
self.authorize(request)
self.process(request)
def authenticate(self, request):
print "===in authenticate==="
def authorize(self, request):
print "===in authorize==="
def process(self, request):
print "===in process==="
def test_method1(sefi, request):
print "===in test_method1==="
handler = MyRequestHandler()
#####1) Start recording (put mock in record mode) #####
m = mox.Mox()
m.StubOutWithMock(handler, "authenticate")#stubout the method 'authenticate' and replace with a mock object named 'authenticate'
m.StubOutWithMock(handler, "authorize")#stubout the method 'authorize' and replace with a mock object named 'authorize'
handler.authenticate(mox.IsA(int))#record the behavior you expect by calling the expected methods on the mock object
handler.authorize(mox.IsA(int))#record the behavior you expect by calling the expected methods on the mock object
handler.authorize(mox.IsA(int)).AndReturn("\nReturn me when calling 'authorize' method \n\n")
handler.authorize(mox.IsA(int)).AndRaise(Exception("Got error \n\n"))
handler.authorize("saju").AndReturn("\nCalled me with string argument 'saju'\n\n")
#####2) Stop recording (put mock objects in reply mode) and start testing #####
m.ReplayAll()
handler.authenticate(1)#Testing method call with int value
handler.authorize(1)#Testing method call with int value
print handler.authorize(1)#Testing the return
try:
handler.authorize(1)#testing the exception raise, we should add try, catch here, otherwise execution stop here
except Exception as ex:
print ex
pass
print "\n\n---here---"
print handler.authorize('saju')#Testing the method with string argument 'saju'
#####3) Verify that we played all recorded things #####
m.VerifyAll()#verify that all recorded (expected) interactions occurred
#####4) UnsetStubs #####
m.UnsetStubs()
import mox
class MyRequestHandler(object):
def authenticate(self, request):
print "===in authenticate==="
handler = MyRequestHandler()
#####1) Start recording (put mock in record mode) #####
m = mox.Mox()
m.StubOutWithMock(handler, "authenticate")#stubout the method 'authenticate' and replace with a mock object named 'authenticate'
#####2) Stop recording (put mock objects in reply mode) #####
m.ReplayAll()
#####3) Verify that we played all recorded things #####
m.VerifyAll()
#####4) UnsetStubs #####
m.UnsetStubs()
2)
import mox
class MyRequestHandler(object):
def handle_request(self, request):
self.authenticate(request)
self.authorize(request)
self.process(request)
def authenticate(self, request):
print "===in authenticate==="
def authorize(self, request):
print "===in authorize==="
def process(self, request):
print "===in process==="
def test_method1(sefi, request):
print "===in test_method1==="
handler = MyRequestHandler()
#####1) Start recording (put mock in record mode) #####
m = mox.Mox()
m.StubOutWithMock(handler, "authenticate")#stubout the method 'authenticate' and replace with a mock object named 'authenticate'
m.StubOutWithMock(handler, "authorize")#stubout the method 'authorize' and replace with a mock object named 'authorize'
handler.authenticate(mox.IsA(int))#record the behavior you expect by calling the expected methods on the mock object
handler.authorize(mox.IsA(int))#record the behavior you expect by calling the expected methods on the mock object
handler.authorize(mox.IsA(int)).AndReturn("\nReturn me when calling 'authorize' method \n\n")
handler.authorize(mox.IsA(int)).AndRaise(Exception("Got error \n\n"))
handler.authorize("saju").AndReturn("\nCalled me with string argument 'saju'\n\n")
#####2) Stop recording (put mock objects in reply mode) and start testing #####
m.ReplayAll()
handler.authenticate(1)#Testing method call with int value
handler.authorize(1)#Testing method call with int value
print handler.authorize(1)#Testing the return
try:
handler.authorize(1)#testing the exception raise, we should add try, catch here, otherwise execution stop here
except Exception as ex:
print ex
pass
print "\n\n---here---"
print handler.authorize('saju')#Testing the method with string argument 'saju'
#####3) Verify that we played all recorded things #####
m.VerifyAll()#verify that all recorded (expected) interactions occurred
#####4) UnsetStubs #####
m.UnsetStubs()
https://code.google.com/p/pymox/wiki/MoxDocumentation
ReplyDeletehttps://code.google.com/p/pymox/wiki/MoxRecipes
https://github.com/openstack/python-neutronclient/blob/master/neutronclient/tests/unit/test_cli20.py