单元测试通用处理程序

时间:2014-12-28 08:43:20

标签: asp.net unit-testing generic-handler

我如何测试" ProcessRequest"的返回值?具有单元Test的通用处理程序中的方法?

 public class Handler1 : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

尝试使用SimpleWorkerRequest创建测试HttpContext,而不是使用mock:

SimpleWorkerRequest testRequest = new SimpleWorkerRequest("","","", null, new StringWriter());
HttpContext testContext = new HttpContext(testRequest);
HttpContext.Current = testContext;

然后您可以创建处理程序并为ProcessRequest方法提供testContext:

var handler = new Handler1();
handler.ProcessRequest(testContext);

然后你可以检查HttpContext.Current.Response来判断你的测试。

更新:

我附上了工作单元测试的完整示例(OutputFilterStream的实现取自here):

    [TestFixture]
    [Category("Unit")]
    public class WhenProcessingRequest
    {
        public class Handler1 : IHttpHandler
        {

            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write("Hello World");
            }

            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }

        public class OutputFilterStream : Stream
        {
            private readonly Stream InnerStream;
            private readonly MemoryStream CopyStream;

            public OutputFilterStream(Stream inner)
            {
                this.InnerStream = inner;
                this.CopyStream = new MemoryStream();
            }

            public string ReadStream()
            {
                lock (this.InnerStream)
                {
                    if (this.CopyStream.Length <= 0L ||
                        !this.CopyStream.CanRead ||
                        !this.CopyStream.CanSeek)
                    {
                        return String.Empty;
                    }

                    long pos = this.CopyStream.Position;
                    this.CopyStream.Position = 0L;
                    try
                    {
                        return new StreamReader(this.CopyStream).ReadToEnd();
                    }
                    finally
                    {
                        try
                        {
                            this.CopyStream.Position = pos;
                        }
                        catch { }
                    }
                }
            }


            public override bool CanRead
            {
                get { return this.InnerStream.CanRead; }
            }

            public override bool CanSeek
            {
                get { return this.InnerStream.CanSeek; }
            }

            public override bool CanWrite
            {
                get { return this.InnerStream.CanWrite; }
            }

            public override void Flush()
            {
                this.InnerStream.Flush();
            }

            public override long Length
            {
                get { return this.InnerStream.Length; }
            }

            public override long Position
            {
                get { return this.InnerStream.Position; }
                set { this.CopyStream.Position = this.InnerStream.Position = value; }
            }

            public override int Read(byte[] buffer, int offset, int count)
            {
                return this.InnerStream.Read(buffer, offset, count);
            }

            public override long Seek(long offset, SeekOrigin origin)
            {
                this.CopyStream.Seek(offset, origin);
                return this.InnerStream.Seek(offset, origin);
            }

            public override void SetLength(long value)
            {
                this.CopyStream.SetLength(value);
                this.InnerStream.SetLength(value);
            }

            public override void Write(byte[] buffer, int offset, int count)
            {
                this.CopyStream.Write(buffer, offset, count);
                this.InnerStream.Write(buffer, offset, count);
            }
        }


        [Test]
        public void should_write_response()
        {
            //arrange
            SimpleWorkerRequest testRequest = new SimpleWorkerRequest("", "", "", null, new StringWriter());                
            HttpContext testContext = new HttpContext(testRequest);                
            testContext.Response.Filter = new OutputFilterStream(testContext.Response.Filter);                

            //act
            var handler = new Handler1();
            handler.ProcessRequest(testContext);                
            testContext.Response.End();//end request here(if it is not done in your Handler1)

            //assert
            var result = ((OutputFilterStream)testContext.Response.Filter).ReadStream();
            Assert.AreEqual("Hello World", result);
        }
    }