如何模拟HttpContext.User

编程入门 行业动态 更新时间:2024-10-27 04:35:37
本文介绍了如何模拟HttpContext.User的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在研究Asp MVC 5项目,并且尝试设置一个模拟程序以在控制器中返回自定义主体.我已经搜索并尝试了不同的建议方法,但是它们都不起作用.

I am working on a Asp MVC 5 project and I am trying to setup a mock to return a custom principal within a controller. I have search and tried different approach suggested but none of them works.

我有一个BaseController,我的所有控制器都继承自该控制器. BaseController具有一个User属性,该属性在getter中返回HttpContext.User.在项目中调用HttpContext.user时返回一个值,但在从单元测试项目中调用时返回NULL.

I have a BaseController which all my controllers inherit from. The BaseController has a User property which return HttpContext.User in the getter. The HttpContext.user returns a value when called within the project but return a null when call from a unit test project.

BaseController

public class BaseController : Controller { protected virtual new CustomPrincipal User { get { return HttpContext.User as CustomPrincipal; } ***<== Line with issue*** } }

自定义主体

public class CustomPrincipal : IPrincipal, ICustomPrincipal { public IIdentity Identity { get; private set; } public string UserId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public bool IsStoreUser { get; set; } public CustomPrincipal(string username) { this.Identity = new GenericIdentity(username); } }

控制器

public class DocumentsController : BaseController { public ViewResult ViewDocuments() { var userType = User.IsStoreUser ? UserType.StoreUser : UserType.Corporate; ***<== User is null when calling from a unit test.*** } }

测试用例

[Test] public void ViewDocuments_WhenCalled_ShouldReturnViewModel() { // Arrange var principal = new CustomPrincipal("2038786"); principal.UserId = "2038786"; principal.FirstName = "Test"; principal.LastName = "User"; principal.IsStoreUser = true; var _mockController = new Mock<DocumentsController>(new UnitOfWork(_context)) { CallBase = true }; _mockController.Setup(u => u.User).Returns(principal); ***<== Error - "Invalid setup on a non-virtual (overridable in VB) member: u => u.User"*** // Act var result = _controller.ViewDocuments(); }

我正在使用nUnit和Moq创建模拟对象,但是我不确定自己做错了什么.我需要在DocumentControl中模拟User.IsStore的返回值,以在测试中创建的自定义主体对象中返回IsStore的值.

I'm using nUnit and Moq to create the mock object but I not sure what I'm doing wrong. I need to mock the return of User.IsStore in the DocumentControl to return the value of IsStore in the custom principal object i created in the test.

推荐答案

模拟http上下文

private class MockHttpContext : HttpContextBase { private readonly IPrincipal user; public MockHttpContext(IPrincipal principal) { this.user = principal; } public override IPrincipal User { get { return user; } set { base.User = value; } } }

相应安排测试.

[Test] public void ViewDocuments_WhenCalled_ShouldReturnViewModel() { // Arrange var principal = new CustomPrincipal("2038786"); principal.UserId = "2038786"; principal.FirstName = "Test"; principal.LastName = "User"; principal.IsStoreUser = true; var mockUoW = new Mock<IUnitOfWork>(); //...setup UoW dependency if needed var controller = new DocumentsController(mockUoW.Object); controller.ControllerContext = new ControllerContext { Controller = controller, HttpContext = new MockHttpContext(principal) }; // Act var result = controller.ViewDocuments(); //Assert //...assertions }

不要模拟正在测试的系统.模拟其依赖项.

Don't mock system under test. Mock its dependencies.

更多推荐

如何模拟HttpContext.User

本文发布于:2023-11-16 09:44:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:HttpContext   User

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!