Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
below is the method i tried to test but it fails for Employee.CurrentEmployee.IsManager this is a boolean value being fetched from a nested class which is dependent on the session so when i run unit test for this it fails cause there is no session created while i am running unit test so i tried mocking the object

public ActionResult IndexCompleted(PlanModel model)
            int PlanID = model.Plan.PlanID;
            ViewBag.Title = "Plan Number: " + PlanID;
            //Only managers can access the plans (the bride now works at the chapel)
            if ((PlanID == 387484 || PlanID == 765675 || PlanID == 872095 || PlanID == 900535) && !Employee.CurrentEmployee.IsManager)
                return Content("Unauthorized");
            return View(model);

What I have tried:

i am trying to create a mock object butfailing to create it cause i don't have a interface
        public void FetchingPlanModelObjectIfYouAreManagerElseUnauthorized()
            PlanController planController = new PlanController();
            PlanModel planmodel = new PlanModel()
                Plan = new LittleChapel.Plan()
                    PlanID = 387484
            Mock<iemployee> objMock = new Mock<iemployee>();
            objMock.Setup(x => Employee.CurrentEmployee.IsManager).Returns(true);
            //objMock.SetupProperty(x => x.IsManager,true);
            var plan = planController.IndexCompleted(planmodel) as ViewResult;
            Assert.AreEqual(planmodel, plan.ViewData.Model);
Updated 28-Aug-16 21:43pm
Bernhard Hiller 29-Aug-16 3:45am    
That is, the interface "iemployee" does not exist? Or something else? Tell us the verbatim error message, and is it a compile-time error or run-time error?
Scyldshefing 3-Nov-16 3:23am    
Given that Employee implements IEmployee and both these classes are defined, how are you injecting the mock object (objMock.Object) into the code that is being tested. Your snippet does not show the declaration of the Employee object in the code under test.
To do this sort of thing (Dependency Injection) I use the Microsoft Unityframework ( )

1 solution

you have some magic numbers there in your controller code. Why don't you swap this out in some extra class, thereby you get some interface you can mock against.

void Main()
	var a = new PlanAuthorizationProvider();
	var result = a.IsAuthorized(true, 387484);
	//result.Dump();   // this is for linqpad 

public interface IPlanAuthorizationProvider
  bool IsAuthorized(bool isManager, int planId);

public class PlanAuthorizationProvider : IPlanAuthorizationProvider
  public bool IsAuthorized (bool isManager, int planId)
   // this goes in your app.config
   //  < appSettings >
   //    < add key = "managerOnlyPlanIds" value = "387484,765675,872095,900535" />
   //  </ appSettings >
   //string managerOnlyPlantIds = ConfigurationManager.AppSettings["managerOnlyPlanIds"];
   string managerOnlyPlanIds = "387484,765675,872095,900535";
   var listOfManagerOnlyPlanIds = 
   if (listOfManagerOnlyPlanIds .Contains(planId))
     return isManager;
     return true;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900