Mocker passthrough

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.

  2. def testRetrieveServicesList(self):
  3. dataSource = ServersInfoService()
  4. mockSource = self.mocker.proxy(dataSource)
  6. mockSource.srv_getServicesList()
  7. self.mocker.passthrough()
  9. self.mocker.replay()
  11. retValue = clientLib.retrieveServicesList(mockSource)
  12. retValue2 = clientLib.retrieveServicesList(mockSource)
  13. 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.

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • online poker game

    Great blog site here! Also your site loads up very fast! What host are you using? Can I get your affiliate link to your host? (send it please to I wish my website loaded up as fast as yours lol… online poker game