Create Unit Test Methods for Entities using Shim and Fakes in ASP.NET

See following steps: 

Step 1:
Create Request object for method.

Request object should have all necessary input parameters to pass.

Step 2:
Create a response object.

Response object should have all necessary values which will cover test method.

Step 3: Create a Shim client instance.

  1. ShimClientClassName.Constructor = (X) => { new ShimClientClassName (); };   
Step 4: Create a context for that particular client.

Here ClientServiceName is a TserviceClient And ClientClassName is a TserviceTypeClient.
  1. var systemContext = EntityFakeHelper.StubSystemContext<ClientServiceName, ClientClassName>();   
Step 5: Create Shim response for that particular client method.

Add Fakes client Namespace. (goto client class).

Append “Shim” before class name e.g ShimClientClassName.

Find method name and click ctrl space.
  1. ShimClientClassName.AllInstances.MethodName = (x, y) => Task.FromResult(objResp);   
Add fake logging.

Step 6: Call method you want to unit test.
  1. var result = await service.ResetPasswordAsync(request);   
Step 7: Assert based on the response or method for we writing unit test.
  1. Assert.AreEqual("123", result.ResultCode);   
Refer below example:
  1. [TestMethod]  
  2.   
  3. public async Task ResetPassword_SuccessIsNo() {  
  4.     using(ShimsContext.Create()) {  
  5.         //Arrange   
  6.         BusinessServiceRequest < AccountPasswordResetRequest > request = new BusinessServiceRequest < AccountPasswordResetRequest > ();  
  7.         request.InteractionRecordCategory1 = "";  
  8.         request.InteractionRecordCategory2 = "";  
  9.         request.InteractionRecordId = "";  
  10.         request.Payload = new AccountPasswordResetRequest()  
  11.   
  12.         {  
  13.             Guid = "BE0E-4CDE-B336-8FE455555444",  
  14.             Password = "P@ssw0rd123",  
  15.             PasswordConfirmation = "Psswrd1234",  
  16.             UserID = "[email protected]"  
  17.         };  
  18.         ResetCustomerPasswordResponse objResp = new ResetCustomerPasswordResponse();  
  19.   
  20.         ShimPortTypeClient.Constructor = (X) = > {  
  21.             new ShimPortTypeClient();  
  22.         };  
  23.         var systemContext = EntityFakeHelper.StubSystemContext < PortType,  
  24.             ShimPortTypeClient > ();  
  25.   
  26.         ShimShimPortTypeClient.AllInstances.ResetCustomerPasswordAsyncResetCustomerPasswordRequest = (x, y) = > Task.FromResult(objResp);  
  27.   
  28.         //Mock ISystemContext   
  29.   
  30.         AuthenticationService service = new AuthenticationService(systemContext);#region Fake Logging  
  31.   
  32.         Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Fakes.ShimExceptionPolicy.HandleExceptionExceptionString = (a1, a2) = > {  
  33.             return true;  
  34.         };  
  35.   
  36.         Common.Diagnostics.Fakes.ShimDiagnosticLogger.AllInstances.WriteTraceDiagnosticCategoryTraceEventTypeDiagnosticCorrelationExceptionStringObjectArray = (a1, a2, a3, a4, a5, a6, a7) = > {  
  37.             return;  
  38.         };  
  39.  
  40.         #endregion  
  41.   
  42.         //Act   
  43.   
  44.         var result = await service.ResetPasswordAsync(request);  
  45.         //Assert   
  46.   
  47.         Assert.AreEqual("1234", result.ResultCode);  
  48.   
  49.         Assert.AreEqual("Shim return this code", result.ResultDescription);  
  50.   
  51.     }  
  52.   
  53. }  

Ebook Download
View all
Learn
View all