mercredi 22 juin 2016

Facing issue with FakeItEasy and NUNIT

I am writing unit test cases using NUnit and FakeItEasy.

I have faced issue with passing Fake session to Controller. We are doing logging for every call to controller.

Here is my controller Code:

  [HttpGet]
        public async Task<ActionResult> NewOrganizationView()
        {
            using (var activity = new LogActivityScope(1, LogActivyScopeOptions.Boundries, "NewOrganizationView [{0}]", ((NMCSession)Session.Contents["NMCSession"]).LastName))
            {
                ViewData["ActivityId"] = System.Diagnostics.Trace.CorrelationManager.ActivityId.ToString();
                try
                {
                    NMSPlatform.Organization userOrganization = await _nmsPlatformClient.GetNMSPlatformDataAsync<NMSPlatform.Organization>(Session, "/NMS/Platform/UserManagementSvc/v1/Organizations/" + ((NMC.Common.NMCSession)Session["NMCSession"]).OrganizationUID);
                    NMCLog.LogInformation("Returning the View");
                    ViewData["PartnerUID"] = userOrganization.DevelopmentPartnerUID;
                    return View("NewOrganization");
                }
                catch (Exception ex)
                {
                    NMCLog.LogException(ex);
                    return View("Error", _platformExceptionHandler.HandleException(ex));
                }
            }
        }

I have tried with the below test Class and Test method.

[TestFixture]
    public class PlatformOrganizationControllerTest
    {
        string url;
        IEnumerable<Organization> Organization;

        IEnumerable<Organization> foundOrganization = null;
        HttpSessionStateBase fakeHttpSession;
        HttpContextBase fakeHttpContext;
       INMSPlatformClient nmsFake;
        ControllerContext ControllerContext;
        PlatformOrganizationController myController;
        bool isFakeInitialized = false;

        public void InitializeFake()
        {
            fakeHttpSession = A.Fake<HttpSessionStateBase>();
            fakeHttpContext = A.Fake<HttpContextBase>();
            nmsFake = A.Fake<INMSPlatformClient>();
            A.CallTo(() => fakeHttpContext.Session).Returns(fakeHttpSession);
            ControllerContext = new ControllerContext(fakeHttpContext, new RouteData(), A.Fake<ControllerBase>());
            myController = new PlatformOrganizationController(nmsFake);
            isFakeInitialized = true;

            fakeHttpSession.Contents["NMCSession"] = new NMCSession()
            {
                DefaultOrganizationUID = 1,
                FirstName = "system",
                Login = "sys.admin",
                LastName = "Admin",
                IsMultiAccountViewable = true,
                OrganizationUID = 1,
                OrganizationName = "NMC"
            };
            myController.ControllerContext = ControllerContext;
        }

        [Test]
        public async Task NewOrganizationView_Success_Test1()
        {
            if (!isFakeInitialized)
                InitializeFake();

            var OrgGuid=1;
            url = "/NMS/Platform/UserManagementSvc/v1/Organizations/"+OrgGuid;

            var result =  await myController.NewOrganizationView();

            Assert.NotNull(result);
            Assert.AreEqual(typeof(System.Web.Mvc.ViewResult), ((System.Web.Mvc.ViewResult)(result)));
            Assert.AreEqual("NewOrganization", ((System.Web.Mvc.ViewResultBase)(result)).ViewName);
        }
    }
}

Can some one please help me how can i pass fake session to the controller for logging. Please suggest any alternative framework if it works.

Aucun commentaire:

Enregistrer un commentaire