I am new to writing a unit test case. I am getting error on User.Identity. I saw mocking is the resolution for that and I tried which is not working in my case. I have added my code
My Controller
public ActionResult CreateStage ( EnthiranStageViewModel enthiranStage ) { if ( ModelState.IsValid ) { Stage stage = enthiran.Insert_Stage(enthiranStage); //logging Stage Creation util.ApplicationLog(new ViewModel.Logs.ApplicationLogViewModel { GameCategorys = GameCategory.Enthiran, Event = Events.EnthiranStageCreation, SessionAttemptId = null, UserId = User.Identity.GetUserId<int>( ), OptionalParameter1 = enthiranStage.GameId, OptionalParameter2 = stage.Id, Description = "Enthiran stage created" }); return RedirectToAction("Stages", new { id = stage.GameId }); } return View( ); } and below is my test case
[TestMethod( )] public void createStage ( ) { EnthiranStageViewModel enthiranStage = new EnthiranStageViewModel { StageType=0, TriggerBeginType = Akton.Areas.Challenge.Models.TriggerType.Manual, TriggerEndType= Akton.Areas.Challenge.Models.TriggerType.Manual, TimeLimit = new TimeSpan(9, 6, 13), TriggerBeginTime= new DateTime(2016, 09, 3, 9, 6, 13), TriggerEndTime= new DateTime(2016, 09, 3, 9, 6, 13), StartValueType= Akton.Areas.Challenge.Models.StartValueType.Global, StageDate= new DateTime(2016, 09, 3, 9, 6, 13), Proforma=25, GameId=19, CreatedTime=new DateTime(2016, 09, 3, 9, 6, 13), UpdatedTime= new DateTime(2016, 09, 3, 9, 6, 13), StageName="Test", }; EnthiranController controller = new EnthiranController( ); JsonResult actual = controller.CreateStage(enthiranStage) as JsonResult; var result = actual.Data; Assert.AreEqual("{ success = True }", result.ToString( )); } Here I have to pass the userId in the ViewModel.Logs.ApplicationLogViewModel, I have no idea how to do that.
How do I get the userId which is passing through applicationLogViewModel?
actualvariable to benullUser.Identitybut theUserproperty of the controller was not setup/arranged in the test method.