Create Unit Test Methods for Entities using Shim and Fakes

Look at given below 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);  
  2. Refer below example: [TestMethod]  
  3. public async Task ResetPassword_SuccessIsNo() {  
  4.     using(ShimsContext.Create()) {  
  5.         BusinessServiceRequest < AccountPasswordResetRequest > request = new BusinessServiceRequest < AccountPasswordResetRequest > ();  
  6.         request.InteractionRecordCategory1 = "";  
  7.         request.InteractionRecordCategory2 = "";  
  8.         request.InteractionRecordId = "";  
  9.         request.Payload = new AccountPasswordResetRequest() {  
  10.             Guid = "BE0E-4CDE-B336-8FE455555444", Password = "P@ssw0rd123", PasswordConfirmation = "Psswrd1234", UserID = "[email protected]"  
  11.         };  
  12.         ResetCustomerPasswordResponse objResp = new ResetCustomerPasswordResponse();  
  13.         ShimPortTypeClient.Constructor = (X) = > {  
  14.             new ShimPortTypeClient();  
  15.         };  
  16.         var systemContext = EntityFakeHelper.StubSystemContext < PortType,  
  17.             ShimPortTypeClient > ();  
  18.         ShimShimPortTypeClient.AllInstances.ResetCustomerPasswordAsyncResetCustomerPasswordRequest = (x, y) = > Task.FromResult(objResp);  
  19.         AuthenticationService service = new AuthenticationService(systemContext);#region Fake Logging  
  20.         Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Fakes.ShimExceptionPolicy.HandleExceptionExceptionString = (a1, a2) = > {  
  21.             return true;  
  22.         };  
  23.         Common.Diagnostics.Fakes.ShimDiagnosticLogger.AllInstances.WriteTraceDiagnosticCategoryTraceEventTypeDiagnosticCorrelationExceptionStringObjectArray = (a1, a2, a3, a4, a5, a6, a7) = > {  
  24.             return;  
  25.         };#endregion  
  26.         var result = await service.ResetPasswordAsync(request);  
  27.         Assert.AreEqual("1234", result.ResultCode);  
  28.         Assert.AreEqual("Shim return this code", result.ResultDescription);  
  29.     }  
  30. }