Custom Search
Showing posts with label python mox. Show all posts
Showing posts with label python mox. Show all posts

Wednesday, November 26, 2014

Python mox mock Unit Test StubOutWithMock example

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()




Python mox mock unittest MultipleTimes example

1)
Without MultipleTimes

class Time:
    def get_time():
        pass


import mox

## Start recording (start record mode)
t=mox.MockObject(Time)

t.get_time().AndReturn("you can call me only once")


## Stop recording (start reply mode)

mox.Replay(t)

t.get_time()

t.get_time()


* Here You can call the method "get_time" only once 




2)
With MultipleTimes

class Time:
    def get_time():
        pass

import mox

t=mox.MockObject(Time)

t.get_time().MultipleTimes().AndReturn("you can call me multiple times")

mox.Replay(t)

t.get_time()

t.get_time()

* Here You can call the method "get_time" multiple times