因为在HttpContext的.NET MVC的测试我控制器抛出错误 [英] Testing My Controller throw error because of HttpContext in .net MVC

查看:275
本文介绍了因为在HttpContext的.NET MVC的测试我控制器抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有叫的HttpContext像一个控制器:

I have a Controller that call HttpContext like:

[Authorize(Roles = "Administrador")]
public class ApuradorController : Controller
{
    private readonly Questiona2011Context _context = new Questiona2011Context();

    private readonly AuthenticationService _authenticationService = new AuthenticationService();
}

HttpContext的是呼叫AuthenticationService类:

The HttpContext is call in AuthenticationService class:

public class AuthenticationService
{
    private IPrincipal _user = HttpContext.Current.User;

    ...
}

在我的项目中,IM测试控制器时,我实例一个错误抛出私人的IPrincipal _user = HttpContext.Current.User控制器; 行:对象引用不设置到对象的实例。

In my project where im test the controllers when I Instance the Controller a error is thrown in private IPrincipal _user = HttpContext.Current.User; Line: Object reference not set to an instance of an object.

什么我需要测试我的控制器?

What do I need to test my controllers?

推荐答案

你怀念的是如何设计的ASP.NET MVC项目测试知识的主要事情。

The main thing that you miss is the knowledge of how to design ASP.NET MVC project for testing.

您应该设计自己的控制器使用依赖注入。也就是说,控制器不应该用具体实施的AuthenticationService的,但使用IAuthenticationService,具体的实现,它在运行时将提供。现在,在创建控制器时,该的AuthenticationService还创建。但是,在测试场景中,HttpContext的是零和创建的AuthenticationService失败与NullReference例外。如果您设计通过接口,在测试的目的,你会提供假执行的AuthenticationService到控制器,它不会抛出异常。

You should design your controller to use dependency injection. That is, controllers should not be using concrete implementation of AuthenticationService, but using IAuthenticationService, concrete implementation of which will be supplied in runtime. For now, when the controller is created, the AuthenticationService is also created. But in test scenario, HttpContext is null and Creating AuthenticationService is failing with NullReference exception. If you design that via interface, in testing purposes you will supply fake implementation of AuthenticationService to controller, and it will not throw exception.

public interface IAuthenticationService
{
    IPrincipal User {get;}
}

public class AuthenticationService : IAuthenticationService
{
    private IPrincipal _user = HttpContext.Current.User;

    ...
}

//the controller
[Authorize(Roles = "Administrador")]
public class ApuradorController : Controller
{
    private readonly Questiona2011Context _context = new Questiona2011Context();

    private readonly IAuthenticationService _authenticationService;

    public ApuradorController(IAuthenticationService authenticationService)
    {
         _authenticationService = authenticationService;
    }
}

在测试场景中,你可以使用假冒IAuthenticationService执行一些嘲讽的库,例如 MOQ 。并通过嘲讽为它供给值

In test scenario, you could use some mocking library for fake IAuthenticationService implementation, for example moq. And supply value for it via mocking

var mockAuthenticationService = new Mock<IAuthenticationService>();
//setup mockAuthenticationService

var controller = new ApuradorController(mockAuthenticationService.Object);

这一次也不会抛出异常。

This time it will not throw exceptions.

以上提到的信息不是有益的,如果你不理解的单元测试项目的设计原则。对于快速启动,读取链接。为进一步阅读,约asp.net mvc的地址簿,我会reccommend那些由史蒂芬·桑德森。单元测试的控制器设计的主要思想是,你应该有提供假的组件,控制器,假仓库,服务等方面的能力,并留下真实的控制器进行了单元测试的唯一部分。然后测试控制器iteractions那些伪装的部分。单元测试是指测试的相互作用。如果相互作用是正确的,他们将是正确的对这些部件的真正实现。如果他们错了,测试失败。

The information mentioned above is not helpful if you do not understand principles of unit testable project design. For the quick start, read this link. For the further reading, address books about asp.net mvc, i would reccommend those by steven sanderson. The main idea of unit testable controller design is that you should have the ability to supply fake components to the controller, fake repositories, services, etc and leave real only the part of controller that is unit tested. Then test controller iteractions with those fake parts. The unit testing means testing that interactions. If interactions are correct, they will be correct with real implementations of those components. If they're wrong, test fails.

这篇关于因为在HttpContext的.NET MVC的测试我控制器抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆