I have came across this several times in forum (telerik) on how I can really fake an item yet I don’t want to pass the instance as an argument. Ideally, this is not a best design but there are third- partly libraries that you have little control over how its written. Anyway, whatever the case might be. This post will show you how you can achieve the above using JustMock. I will be using MSpec in conjunction for the example.
So here is the sample class that I am going to use:
Here CheckUser is calling LoginService.ValidateUser that we are going replace with a fake call. Also here is the LoginService class that is not much significant just throws exception when ValidateUser is called that will even confirm if the correct variant is invoked.
Here is the specification that I wrote with Justmock and MSpec
This concludes that if you have a mock setup and you cant pass the instance via dependency injection, JustMock will still try to find the closest mock setup for arguments that it will replace the original call with when invoked. There is no extra setup call to be associated in that regard and keeps the tool more out of your way.
In addition I am attaching the sample project to let you have a look. Also you will be needing JustMock full edition installed to try this out.
Copyright © 2017, 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.