单元测试的一个HttpApplication [英] Unit testing an HttpApplication

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

问题描述

我有一个HttpApplication派生类,增加了一些额外的功能。我的地方,我需要进行单元测试这些功能来看,这意味着我必须要能够创建HttpApplication的,假的请求的新实例,并检索响应对象。

I have a class derived from HttpApplication that adds some extra features. I'm to the point where I need to unit test these features, which means I have to be able to create a new instance of the HttpApplication, fake a request, and retrieve the response object.

我去究竟是如何单元测试一个HttpApplication对象?我使用起订量的时刻,但我不知道如何设置所需的模仿对象。

How exactly do I go about unit testing an HttpApplication object? I'm using Moq at the moment, but I have no idea how to set up the required mock object.

推荐答案

不幸的是,这是特别不容易的事,因为一个HttpApplication本身不适合非常方便的嘲讽;没有接口嘲笑反对,大多数的方法都没有标记为虚拟的。

Unfortunately, this isn't particularly easy to do, as the HttpApplication doesn't lend itself to mocking very easily; there is no interface to mock against and most of the methods aren't marked as virtual.

我最近有型Htt prequest和HttpWebResponse类似的问题。最后,我去的解决方案是创建一个直的直通包装的方法,我想用:

I recently had a similar problem with HttpRequest and HttpWebResponse. In the end, the solution I went for was to create a straight "pass-through" wrapper for the methods I wanted to use:

public class HttpWebRequestWrapper : IHttpWebRequestWrapper
    {
        private HttpWebRequest httpWebRequest;

        public HttpWebRequestWrapper(Uri url)
        {
            this.httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
        }

        public Stream GetRequestStream()
        {
            return this.httpWebRequest.GetRequestStream();
        }

        public IHttpWebResponseWrapper GetResponse()
        {
            return new HttpWebResponseWrapper(this.httpWebRequest.GetResponse());
        }

        public Int64 ContentLength
        {
            get { return this.httpWebRequest.ContentLength; }
            set { this.httpWebRequest.ContentLength = value; }
        }

        public string Method 
        {
            get { return this.httpWebRequest.Method; }
            set { this.httpWebRequest.Method = value; }
        }

        public string ContentType
        {
            get { return this.httpWebRequest.ContentType; }
            set { this.httpWebRequest.ContentType = value; }
        }
}

等等,等等

这让我嘲笑我对自己的包装接口。不一定是世界上最优雅的东西,但嘲讽了一些框架的少mockable部分的一个非常有用的方法。

This let me mock against my own wrapper interface. Not necessarily the most elegant thing in the world, but a very useful way of mocking out some of the less "mockable" parts of the framework.

您赶去做这虽然之前,这是值得检讨你有什么,看是否有你的测试,将避免你不得不包装类的更好方法。

Before you rush off and do this though, it is worth reviewing what you've got and seeing if there is a better approach to your tests that would avoid you having to wrap classes.

在的情况下的HttpWebRequest,HttpApplication的等,经常有不IMHO

In the case of HttpWebRequest, HttpApplication et al, there often isn't IMHO.

为了设置此包装在模拟(使用上面我HttpWebRequest的例子),那么你做的东西像这样用起订量:

In order to set this wrapper in mock (using my HttpWebRequest example above) you then do stuff like this with Moq:

var mockWebRequest = new Mock<IHttpWebRequestWrapper>();
mockWebRequest.SetupSet<string>(c => c.Method = "POST").Verifiable();
mockWebRequest.SetupSet<string>(c => c.ContentType = "application/x-www-form-urlencoded").Verifiable();
mockWebRequest.SetupSet<int>(c => c.ContentLength = 0).Verifiable();

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

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