Another nice feature in mocker is passthrough. It is perfect when you want to ask if a given call was performed but calling the real object rather than the mock so getting a real result. This is an example.
- def testRetrieveServicesList(self):
- dataSource = ServersInfoService()
- mockSource = self.mocker.proxy(dataSource)
- retValue = clientLib.retrieveServicesList(mockSource)
- retValue2 = clientLib.retrieveServicesList(mockSource)
- self.assertEqual(retValue, retValue2)
The test above is testing the behavior of method which is using a cache. Method clientLib.retrieveServicesList is accesing a dataSource for the first time but the next time it has to get the data from the cache. So I call the method twice in replay mode but I set only one call to the dataSource in the expectations. The first call, passthrough makes sure the call is done in the real object, not the mock.
The cache test should be a separate unit test just to test the getValue/setValue but, as this is Python I try to write at least a simple test for every method, just to make sure there are no typo errors or API errors that a compiler would find out. If I was just testing the cache, I would not need the passthrough but just a mock result. However, using the real result means that retrieveServicesList is going to work with real data and that is what I was looking for now.