In this post, I will start with an MVC sample created from the default template project that is bundled with ASPNET MVC2 installation. This template provides not just a dummy project with folder organized but rather a full running app.The target of this post is to show a mocking of a controller action, in that regard I have picked registration action chosen from the accounts controller of the provided sample that looks similar to:
Here the basic workflow that can be ensured for the target action are:
Accordingly, to cover the first criteria that covers a valid registration. We first created the accounts controller and mocked the registration model.
In order to validate model state, we also need to mock out the ModelStateDictionary to set our expectation:
Then, according to flow, we setup:
Next, we need to pass out our custom MembershipService to the controller and set it to return a valid response ignoring the arguments. We are ignoring the arguments, as here the specific argument is not important and we are concerned more with the desired step that should be completed.
Now, after having a successful registration our next goal is to make sure that developer has written the sign in code correctly and accordingly we do:
To wrap it up and give a test run, we execute the controller with the RegisterModel object that we have created previously.
Finally, we make sure that required calls are made properly.
Now, this is of a rough test, as you can see there are three different types of assert which should spun three different test methods. I left that to the reader :-).
P.S. This example is done using JustMock, by the time of writing this post IgnoreArguments() is still under its way to release [JustMock still in its beta]. The way shown for ignoring arguments can also be done using matcher, but it is to mention that for two or many arguments when we just want to ignore the whole chunk, using a modifier call is more elegant and cleaner to read.
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.