Datetime mocking with pyxmox

Avatar for vbabiy@howsthe.com

Datetime mocking with pyxmox

Published Feb. 11, 2010 by Vitaly Babiy

This is a reference on how to mock out the datetime.now() call in python using mox. Since datetime is a built-in it makes things a little more tricky.

def test_datetime(self):
    now = datetime.datetime.now() # Store it for later use
    m.StubOutWithMock(datetime, 'datetime')

    datetime.datetime.now().AndReturn(now)

    m.ReplayAll()

    datetime.datetime.now()

    m.VerifyAll()
    m.UnsetStubs()

First we will store the current datetime, this has to be done before mocking takes place. Next we use StubOutWitMock to mock out the datetime class, we then record the call to now and have it return our stored now. If you have any question leave them in the comments.

Links:

Written By Vitaly Babiy

Avatar for vbabiy@howsthe.com

Vitaly Babiy is the creator of Howsthe.com (Yes, you can contact him about the service). He is a software engineer at heart, loves working with great technologies like Django and Jquery. Vitaly spends most of his days in python and loves it. Another passion of Vitaly's is learning the business side of things, one of the reason why he started Howsthe.com monitoring service. You can follow him on Twitter

blog comments powered by Disqus

A blog about development, marketing, and design.