Recently, while i was working with a support issue , i found this interesting piece of test code that i would like to share here. This is actually written by Stefan Lieser (clean code developer from Germany forwarded to me by Jan from Telerik Germany). As the title states, it is to mock a specific event for an expected call.
Now Stefan wants to raise an event from WebClient class of System.Net for a download operation. Therefore, first the WebClient class is mocked during setup.
Next the mocking here takes place in two part. First, DownloadDataCompletedEventArgs is mocked to return an expected data when Result property is get.
This is followed by an arrange that will raise the expected event when DownloadDataAsync is invoked.
Finally, the whole test method ends up like:
The above example is done using JustMock SP1. You can further download the code here:
Happy coding !!
Philip Japikse is an international speaker, a Microsoft MVP, ASPInsider, INETA Community Champion, MCSD, CSM/ CSP, and a passionate member of the developer community. Phil has been working with .Net since the first betas, developing software for over 20 years, and heavily involved in the agile community since 2005. Phil also hosts the Hallway Conversations podcast (www.hallwayconversations.com) and serves as the Lead Director for the Cincinnati .Net User’s Group (http://www.cinnug.org). You can follow Phil on twitter via www.twitter.com/skimedic, or read his personal blog at www.skimedic.com/blog.
Copyright © 2016, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks or appropriate markings.