nsigustavo / ludibrio

Platform for test doubles in Python (mocks, stubs, Spy, and dummies)
113 stars 15 forks source link

import feature removing the object #3

Closed hltbra closed 14 years ago

hltbra commented 14 years ago

code::

# coding: utf-8

import unittest
from livefeed.models import FeedItem
from ludibrio import Stub

# FIXME
# precisa pensar em alguma maneira para manipular o datetime.datetime.now()
# porque nem sempre as diferenças de tempos serão válidas.
# Exemplo: 0s - 1s = -1s -> segundos inválidos!

class LiveFeedItemTestCase(unittest.TestCase):
    def setUp(self):
        self.template = 'myapp/templates/template1.html'
        from datetime import datetime
        self.now = datetime(year=2010, month=12, day=20, hour=10, minute=15, second=30)

    def test_um_dia_atras(self):
        from datetime import datetime
        um_dia_atras = datetime(year=self.now.year, month=self.now.month, day=self.now.day-1)
        with Stub() as datetime:
            from datetime import datetime
            datetime.now() >> self.now
        item = FeedItem(template=self.template, date=um_dia_atras)
        self.assertEquals('há 1 dia', item.tempo())

If we remove that from .. import to outside those methods, it does not work!

nsigustavo commented 14 years ago

coding: utf-8

import unittest from livefeed.models import FeedItem from ludibrio import Stub from datetime import datetime

NOW = datetime(year=2010, month=12, day=20, hour=10, minute=15, second=30) UM_DIA_ATRAS = datetime(year=self.now.year, month=self.now.month, day=self.now.day-1)

class LiveFeedItemTestCase(unittest.TestCase): def setUp(self): self.template = 'myapp/templates/template1.html'

def test_um_dia_atras(self):
    with Stub() as datetime:
        from datetime import datetime
        datetime.now() >> NOW
    item = FeedItem(template=self.template, date=UM_DIA_ATRAS)
    self.assertEquals('há 1 dia', item.tempo())