When using unit tests for a class, I get an error when using NUnit. The functionality is basic - check that an item is in the cache, and if not, then create it and put it in:
if (HttpContext.Current.Cache ["Some_Key"] == Faucet } {MyObject = new object (); . } And {myObject = HttpContext.Current.Cache.Get ("Some_Key"); }
When calling from a unit test, it fails when the first cache
arrives at NullReferenceException
in Java, I server I will use to test side code. Is there any such tool that I can use for the C # code? Fake frameworks are mentioned - is this the only way I can test these methods? Are there tools similar to running tests for C #?
In addition, I do not check that cache
is zero because I do not want to specifically type the code for the unit test and assume that it is always valid when running on the server Will happen. Is it valid, or should I add meaningless checks around the cache?
The way to do this is to avoid direct use of HttpContext or other similar classes, after all, you test Trying to do that the HTTP contact works properly (which is Microsoft's job), you are just trying to check when the methods were told to them.
Step (if you just want to know the techniques without digging without the burden of blogs):
-
Create an interface that you can use in your caching Describes methods (things like Getitum, Satyam, Expiryme) Call it ICache or whatever you want
-
Create a class that implements the interface, and passes through the real HTTPTPOntax methods
-
Create a class that implements the same interface, and acts just like a duplicate cache. If you care about saving objects, it can use a dictionary or something
-
Change your original code so that it does not use HTTPPontact anymore, and its Instead of using only ICS, the code will then need to get an example of ICCH - you can either give an example in the manufacturer of your class (this is all dependency injection really) or some Applications that can stick in the variable.
-
In your production app, set the ICc for your real HTText-backed-cache, and in your unit test, set ICech to be Mac cache.
-
Profit!
Comments
Post a Comment