在unit testing中使用Moq模拟认证用户
我们如何使用Moq框架模拟经过身份validation的用户。 使用表单身份validation
我需要为下面的操作编写unit testing
public PartialViewResult MyGoals() { int userid = ((SocialGoalUser)(User.Identity)).UserId; var Goals = goalService.GetMyGoals(userid); return PartialView("_MyGoalsView", Goals); }
我需要在这里模拟userid的值
我曾经使用过这样的东西,也许它可以帮助你:
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;
- 我想让EditFor文本框只接受数字
- C#:枚举中的按位运算符(MVC中的自定义授权)
- C#mvc图像上传resize服务器端
- 找不到方法:’Void Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(Boolean)’
- 如何使用Orchard.Forms插入保存按钮?
- 将DataAnnotations添加到自动生成的DBML类? MVC 2.0 ASP.NET
- LINQ to SQL – 如何有效地对多个条件进行AND或OR搜索
- MVC视图模型inheritance和创建动作
- ASP.Net MVC5和StructureMap4 – 简化方法