News for hackers. Stuff that matters.
Re:Why I don't mock by meric on Wednesday May 28, 2014 @08:44AM (#7809595)
I don't use mocks much, except with regard to the "now" method. There's some code that relies on the current time, I can use mock to mock many values of 'current time' instead of being forced to only test the case where the current time is when I run the unit test. This is especially important when dealing with code involving time zone and day light savings.
Re:Why I don't mock by mpweiher on Wednesday May 28, 2014 @08:48AM (#7809608)
My experience with time is that whenever you build a system that is time-dependent in any meaningful way, you want to have "synthetic time" that you control, which you only feed with real time in a single, controllable place.

Otherwise you get nasty effects, such as time moving on while your system is processing etc.

Re:Why I don't mock by meric on Wednesday May 28, 2014 @09:46AM (#7809840)
I'm guessing you mean like:

    def has_expired(now=None):
        #.. implementation
instead of:

    def has_expired():
        #.. implementation
But at some point you need to call that method somewhere else...

    def get_request_handler(request):
        #...code
        now = timezone.now()
        if has_expired(now=now):
           raise Error403.
And `get_request_handler` still needs to be tested, which I 'stub' the API for getting the current time in the correct timezone within the test for the request. (Taking into account the other comment), using a library called 'mock'. (https://pypi.python.org/pypi/mock)
Re:Why I don't mock by nahname on Wednesday May 28, 2014 @08:58AM (#7809637)
If you are only changing the value of what an API returns, that is not a mock. It is a stub.
Re:Why I don't mock by blt on Wednesday May 28, 2014 @09:55AM (#7809884)
I agree. But another option is to make all the methods that would call now() take an extra parameter of the current time. Talk about making your dependencies explicit...
Re:Why I don't mock by GrinningFool on Wednesday May 28, 2014 @10:03AM (#7809916)
But now you've...

a) changed your interface in a way that invites use cases you never intended (or wanted) to support.

b) added branching code anywhere that requires now()