mocking and aspects

For development of pyherc, I have been using pyDoubles (originally I was using Mock, but it was a bit pain to work with properties). However, I recently started using aspects (with aspyct) and that completely broke call verification with pyDoubles. After a bit trial-and-error, I found mockito-python that plays nicely with aspyct, works with properties and has friendly syntax.

So, why do I want to use aspects with rogue-clone? I got tired writing calls to logging over and over again. With aspects I can just add logging to a method call with a single line of code and everything else is taken care for me (of course I had to write the aspect first, but that didn’t take long).

Now I can write following (from LevelGeneratorFactory):

@logged
def get_generator(self, level_type):

….

return generator

When this is executed and debug logging has been turned on, I’ll get following in pyherc.log:

DEBUG:pyherc.generators.level.generator.LevelGeneratorFactory:get_generator :call: (‘upper crypt’,) : {}
DEBUG:pyherc.generators.level.generator.LevelGeneratorFactory:get_sub_component :call: (‘upper crypt’, [<pyherc.generators.level.partitioners.grid.GridPartitioner object at 0x01AC9330>, <pyherc.generators.level.partitioners.grid.GridPartitioner object at 0x01AC9570>], ‘partitioner’) : {}
DEBUG:pyherc.generators.level.generator.LevelGeneratorFactory:get_sub_component :return: <pyherc.generators.level.partitioners.grid.GridPartitioner object at 0x01AC9570>

DEBUG:pyherc.generators.level.portals.PortalAdderFactory:create_portal_adders :call: (‘upper crypt’,) : {}
DEBUG:pyherc.generators.level.portals.PortalAdderFactory:create_portal_adders :return: []

DEBUG:pyherc.generators.level.generator.LevelGeneratorFactory:get_generator :return: <pyherc.generators.level.generator.LevelGenerator instance at 0x01B86738>

This pretty plainly tells me that somebody wanted instance of LevelGenerator that can be used to generate upper crypts. Also, I notice that this branch of the dungeon seems to be a dead end, since no PortalAdders are created by PortalAdderFactory.

After trying out the third mocking framework so far, I’m pretty happy with the features mockito-python offers. Should I have done some research in the beginning and choose that in the first place? Probably wouldn’t have changed anything, since I didn’t know back then that I would end up using aspects (and this particular AOP-library).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s