对用户身份的空引用
Null reference on user identity
我是编写单元测试用例的新手。我在 User.Identity
上遇到错误。我看到嘲笑是解决这个问题的方法,我试过这对我的情况不起作用。我已经添加了我的代码
我的控制器
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( );
}
下面是我的测试用例
[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( ));
}
这里我必须在 ViewModel.Logs.ApplicationLogViewModel
中传递 userId
,我不知道该怎么做。
如何获取正在通过 applicationLogViewModel
的 userId
?
一种解决方案是更改 EnthiranController
并传递,例如 IUserContext
,如下所示:
public interface IUserContext
{
public IPrincipal User {get;}
}
然后通过构造函数将其传递给控制器,并使用该上下文检索用户。
ctor EnthiranController(IUserContext userContext)
然后稍微更改单元测试以模拟所有这些接口。此外,您可以使用 ActionResult
或 RedirectToRouteResult
代替 JsonResult
,如下例所示。
[TestMethod( )]
public void createStage ( )
{
//arrange
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"
};
Mock<IPrincipal> mockPrincipal = new Mock<IPrincipal>();
//TODO: setup mockPrincipal
Mock<IUserContext> mockUserContext = new Mock<IUserContext>();
mockUserContext.Setup(p => p.User).Returns(mockPrincipal.Object);
EnthiranController controller = new EnthiranController(mockUserContext.Object);
//act
var actual = controller.CreateStage(enthiranStage) as RedirectToRouteResult;
//assert
Assert.IsNotNull(actual);
}
我是编写单元测试用例的新手。我在 User.Identity
上遇到错误。我看到嘲笑是解决这个问题的方法,我试过这对我的情况不起作用。我已经添加了我的代码
我的控制器
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( );
}
下面是我的测试用例
[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( ));
}
这里我必须在 ViewModel.Logs.ApplicationLogViewModel
中传递 userId
,我不知道该怎么做。
如何获取正在通过 applicationLogViewModel
的 userId
?
一种解决方案是更改 EnthiranController
并传递,例如 IUserContext
,如下所示:
public interface IUserContext
{
public IPrincipal User {get;}
}
然后通过构造函数将其传递给控制器,并使用该上下文检索用户。
ctor EnthiranController(IUserContext userContext)
然后稍微更改单元测试以模拟所有这些接口。此外,您可以使用 ActionResult
或 RedirectToRouteResult
代替 JsonResult
,如下例所示。
[TestMethod( )]
public void createStage ( )
{
//arrange
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"
};
Mock<IPrincipal> mockPrincipal = new Mock<IPrincipal>();
//TODO: setup mockPrincipal
Mock<IUserContext> mockUserContext = new Mock<IUserContext>();
mockUserContext.Setup(p => p.User).Returns(mockPrincipal.Object);
EnthiranController controller = new EnthiranController(mockUserContext.Object);
//act
var actual = controller.CreateStage(enthiranStage) as RedirectToRouteResult;
//assert
Assert.IsNotNull(actual);
}