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.
- ShimClientClassName.Constructor = (X) => { new ShimClientClassName (); };
Step 4: Create a context for that particular client.
Here ClientServiceName is a TserviceClient And ClientClassName is a TserviceTypeClient.
- 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.
- ShimClientClassName.AllInstances.MethodName = (x, y) => Task.FromResult(objResp);
Add fake logging.
Step 6: Call method you want to unit test.
- var result = await service.ResetPasswordAsync(request);
Step 7: Assert based on the response or method for we writing unit test.
- Assert.AreEqual("123", result.ResultCode);
Refer below example:
- [TestMethod]
-
- public async Task ResetPassword_SuccessIsNo() {
- using(ShimsContext.Create()) {
-
- BusinessServiceRequest < AccountPasswordResetRequest > request = new BusinessServiceRequest < AccountPasswordResetRequest > ();
- request.InteractionRecordCategory1 = "";
- request.InteractionRecordCategory2 = "";
- request.InteractionRecordId = "";
- request.Payload = new AccountPasswordResetRequest()
-
- {
- Guid = "BE0E-4CDE-B336-8FE455555444",
- Password = "P@ssw0rd123",
- PasswordConfirmation = "Psswrd1234",
- UserID = "[email protected]"
- };
- ResetCustomerPasswordResponse objResp = new ResetCustomerPasswordResponse();
-
- ShimPortTypeClient.Constructor = (X) = > {
- new ShimPortTypeClient();
- };
- var systemContext = EntityFakeHelper.StubSystemContext < PortType,
- ShimPortTypeClient > ();
-
- ShimShimPortTypeClient.AllInstances.ResetCustomerPasswordAsyncResetCustomerPasswordRequest = (x, y) = > Task.FromResult(objResp);
-
-
-
- AuthenticationService service = new AuthenticationService(systemContext);#region Fake Logging
-
- Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Fakes.ShimExceptionPolicy.HandleExceptionExceptionString = (a1, a2) = > {
- return true;
- };
-
- Common.Diagnostics.Fakes.ShimDiagnosticLogger.AllInstances.WriteTraceDiagnosticCategoryTraceEventTypeDiagnosticCorrelationExceptionStringObjectArray = (a1, a2, a3, a4, a5, a6, a7) = > {
- return;
- };
-
- #endregion
-
-
-
- var result = await service.ResetPasswordAsync(request);
-
-
- Assert.AreEqual("1234", result.ResultCode);
-
- Assert.AreEqual("Shim return this code", result.ResultDescription);
-
- }
-
- }