我正在研究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
发布评论