단위 테스트 케이스 작성에 익숙하지 않습니다. User.Identity
에 오류가 발생했습니다. 나는 조롱이 그것을위한 해결책이라고 보았고 나는 나의 경우에는 효과가없는 것을 시도했다. 내가 여기사용자 신원에 대한 널 참조
[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
를 전달하는 내 코드
내 컨트롤러
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();
}
아래 내 테스트 케이스 추가 한, 내가 어떻게 그렇게하는 아무 생각이 없다 .
userId
은 applicationLogViewModel
을 어떻게 통과합니까?
public interface IUserContext
{
public IPrincipal User {get;}
}
다음 컨트롤러 생성자를 통해 그것을 전달하고 사용자를 검색하는 콘텍스트를 사용
확인 https://stackoverflow.com/questions/40230776/how-to-mock-httpcontext-user/40234602#40234602 – Nkosi
확인 http://stackoverflow.com/a/39453132/5233410 – Nkosi
당신은해야 또한 테스트중인 메소드가 뷰 결과를 반환하거나 액션 결과로 리디렉션 할 수 있지만 유닛 테스트가 JSON 결과를 확인하는지 주목하십시오. 'actual' 변수가'null '이됩니다. – Nkosi