Mock authenticated user using Moq in unit testing

后端 未结 1 587
离开以前
离开以前 2021-01-05 00:19

How we can mock the authenticated user using Moq framework. Form Authentication used.

I need to write unit tests for the action below

public PartialV         


        
1条回答
  •  隐瞒了意图╮
    2021-01-05 01:02

    I have used something like that, maybe it helps you:

    var controllerContext = new Mock();
    var principal = new Moq.Mock();
    principal.Setup(p => p.IsInRole("Administrator")).Returns(true);
    principal.SetupGet(x => x.Identity.Name).Returns(userName);
    controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal.Object);
    controller.ControllerContext = controllerContext.Object;
    

    0 讨论(0)
提交回复
热议问题