C#单元测试API调用2 [英] C# unit testing API 2 call

查看:483
本文介绍了C#单元测试API调用2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web API 2 Web服务get方法。里面我使用HttpContext.Current.Request.UserHostAddress。当调用我的控制器方法是直接的诠释,他这不是在充满单元测试,以便与空对象错误。所以,我搜索了如何填写此,发现它与这个问题帮助的情况如下:添加IP地址到HttpRequestMessage



然而,这需要一个服务器名称发送请求。问题是,测试时运行VSExpress将需要运行此API的Web服务,该刚运行测试时,它不会。最重要的是即使是现在看来,这挑选一个随机端口上运行这么喜欢他在上面的链接确实我不能硬编码地址。如何测试我的API 2的方法给出了上述问题?



这是吹起来的时候我只是测试API方法


线

 串IP = HttpContext.Current.Request.UserHostAddress; 





只是让每个人都知道这里是代码

 公共类myController的解决方案:ApiController 
{
私人:HttpRequestBase的HttpRequest;

公共myController的()
{
的HttpRequest =新HttpRequestWrapper(HttpContext.Current.Request)
}

公共myController的(HttpRequestBase HTTP )
{
的HttpRequest = HTTP;
}

公共HttpResponseMessage的get()
{
串IP = httpRequest.UserHostAddress;
}
}



我用的起订量在单元测试:

 模拟< HttpRequestBase> httpRequestMock =新的模拟< HttpRequestBase>(); 

httpRequestMock.Setup(X => x.UserHostAddress).Returns(127.0.0.1);

//然后通过httpRequestMock.Object我控制器构造函数和良好的去


解决方案

通过向的的HttpContext 您引用http://msdn.microsoft.com/en-美国/库/ system.web.httpcontextbase%28V = vs.110%29.aspx相对=nofollow> HttpContextBase 。当你的代码,初始化 HttpContextBase HttpContextWrapper 实例,它是在网络堆栈的默认行为实施。



然而,在您的测试注入您实现只测试所需的方法和行为的自定义 HttpContextBase 的实施。



作为一种高精度的链接:




HttpContextBase类是一个抽象类包含相同
成员HttpContext类。该HttpContextBase类允许
你创建的派生类,就像HttpContext类,但
,你可以自定义和ASP.NET管道之外的工作。
当您执行单元测试,通常使用派生类
实现与满足的情况
你正在测试自定义行为的成员。



I have a web api 2 web service get method. Inside I'm using HttpContext.Current.Request.UserHostAddress. When calling my controller method directly int he unit test this isn't filled in so is errors with null object. So I searched for how to fill this in and found the following which helped with that issue: Add IP address to HttpRequestMessage

However, this needs a server name to send the request to. The problem is that when tests run the VSExpress will need to be running for this API web service, which it won't be when just running the tests. On top of that even if it was it seems it picks a random port to run on so I couldn't hardcode the address like he does in the above link. How can I test my api 2 method given the above issues?

This is the line that blows up when I just test the api method

string ip = HttpContext.Current.Request.UserHostAddress;

[EDIT] Answer

Just so everyone knows here is the solution in code

public class MyController : ApiController
{
    private: HttpRequestBase httpRequest;

    public MyController()
    {
        httpRequest = new HttpRequestWrapper(HttpContext.Current.Request)
    }

    public MyController(HttpRequestBase http)
    {
        httpRequest = http;
    }

    public HttpResponseMessage Get()
    {
        string ip = httpRequest.UserHostAddress;
    }
}

I use Moq in the unit test:

Mock<HttpRequestBase> httpRequestMock = new Mock<HttpRequestBase>();

httpRequestMock.Setup(x => x.UserHostAddress).Returns("127.0.0.1");

// then pass httpRequestMock.Object to my controller ctor and good to go

解决方案

Replace your references to HttpContext by references to HttpContextBase. When in your code, initialize the HttpContextBase with a HttpContextWrapper instance, which is a the default behavior implementation in a web stack.

However in your test inject a custom HttpContextBase implementation where you implement the methods and behaviors needed by your test only.

As precised in the link:

The HttpContextBase class is an abstract class that contains the same members as the HttpContext class. The HttpContextBase class enables you to create derived classes that are like the HttpContext class, but that you can customize and that work outside the ASP.NET pipeline. When you perform unit testing, you typically use a derived class to implement members with customized behavior that fulfills the scenario you are testing.

这篇关于C#单元测试API调用2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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