[TIP] Issue setting up the correct mocking behavior

Jonas Geiregat jonas at geiregat.org
Sat Apr 14 04:41:19 PDT 2012


Hello,

I'm trying to test a function that imports and acts upon a django model.

from apps.market.models import Gig

def create_house(location, date, price):
	house = House(id=None, date, price)
	house.save()

	# calculate some stuff and further expand the house instance
	# for example house.tag.add("some-tag")
	
	# save after calculations
	house.save()

I like to mock out the House module.
This is what I've tried so far, this is a method of a TestCase class:

 @patch('apps.market.models.House')
    def create_house_test(self, MockedHouse):
    
        """ Constants """
        DAYS_FROM_TODAY = 55
        DATE = datetime.date.today() + datetime.timedelta(days=DAYS_FROM_TODAY)
	PRICE = 250000
    
	# A location is also a django module , I'm using factory_boy here for building a 'mocked' location
        location = LocationFactory.build()


        create_house(DATE, PRICE)
        MockedHouse.assert_called_with(None, DATE, PRICE)   
	MockedHouse.save.assert_called_with()


I keep getting an assertionError, the Class is never called.

I'm also wondering if it's possible to inspect MockedHouse to see if it has for example some tags added to it.

Any help is appreciated,

Jonas.




More information about the testing-in-python mailing list