軟件測試中含有HttpContext元素的單元測試
單元測試是在軟件開發過程中要進行的最低級別的測試活動,在單元測試活動中,軟件的獨立單元將在與程序的其他部分相隔離的情況下進行測試。 單元測試不僅僅是作為無錯編碼一種輔助手段在一次性的開發過程中使用,單元測試必須是可重復的,無論是在軟件修改,或是移植到新的運行環境的過程中。因此,所有的測試都必須在整個軟件系統的生命周期中進行維護。
我們在開發WEB項目的時候,一般應用邏輯跟ASPX頁面是分離的項目。應用邏輯一般會是一個DLL組件項目。如果這個組件項目中A方法使用了Session、Cookie等信息的讀寫,則這個方法就很難寫單元測試。
但并不是寫不出來,要寫出來大致思路如下:
目標:
構建一個測試的環境,把需要的Session、Cookie等信息初始化好。 這樣才好做測試。而且這個構建的環境,不應該影響實際功能代碼的編寫。
具體實現來說:
我們要使用Mock技術,但就HttpContext來言,直接mock這個對象會有一個問題,它不具備Session的功能。這時候我們就需要用 Mock 技術來構造一個可以滿足我們需要的環境的原理:這個Mock的機制如下:
用反射機制,構造一個 HttpSessionState 對象(HttpSessionState類的構造函數是internal 的),然后把這個對象跟SimpleWorkerRequest 對象捆綁。
這樣我們就可以 構建了一個滿足自己需要的環境了,即 TestHttpContext 類。
以下是兩個類的實現:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.SessionState;
using System.Web;
using System.Threading;
using System.Globalization;
using System.Collections.Specialized;
using System.Collections;
using System.IO;
using System.Web.Hosting;
using System.Reflection;
namespace TestNamespace
{
public class TestHttpContext
{
private const string ContextKeyAspSession = "AspSession";
private HttpContext context = null;
private TestHttpContext() : base() { }
public TestHttpContext(bool isSecure)
: this()
{
MySessionState myState = new MySessionState(Guid.NewGuid().ToString("N"),
new SessionStateItemCollection(), new HttpStaticObjectsCollection(),
5, true, HttpCookieMode.UseUri, SessionStateMode.InProc, false);
TextWriter tw = new StringWriter();
HttpWorkerRequest wr = new SimpleWorkerRequest("/webapp", "c:\\inetpub\\wwwroot\\webapp\\", "default.aspx", "", tw);
this.context = new HttpContext(wr);
HttpSessionState state = Activator.CreateInstance(
typeof(HttpSessionState),
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance,
null,
new object[] { myState },
CultureInfo.CurrentCulture) as HttpSessionState;
this.context.Items[ContextKeyAspSession] = state;
HttpContext.Current = this.context;
}
public HttpContext Context
{
get
{
return this.context;
}
}
private class WorkerRequest : SimpleWorkerRequest
{
private bool isSecure = false;
public WorkerRequest(string page, string query, TextWriter output, bool isSecure)
: base(page, query, output)
{
this.isSecure = isSecure;
}
public override bool IsSecure()
{
return this.isSecure;
}
}
}
public sealed class MySessionState : IHttpSessionState
{
const int MAX_TIMEOUT = 24 * 60; // Timeout cannot exceed 24 hours.
string pId;
ISessionStateItemCollection pSessionItems;
HttpStaticObjectsCollection pStaticObjects;
int pTimeout;
bool pNewSession;
HttpCookieMode pCookieMode;
SessionStateMode pMode;
bool pAbandon;
bool pIsReadonly;
public MySessionState(string id,
ISessionStateItemCollection sessionItems,
HttpStaticObjectsCollection staticObjects,
int timeout,
bool newSession,
HttpCookieMode cookieMode,
SessionStateMode mode,
bool isReadonly)
{
pId = id;
pSessionItems = sessionItems;
pStaticObjects = staticObjects;
pTimeout = timeout;
pNewSession = newSession;
pCookieMode = cookieMode;
pMode = mode;
pIsReadonly = isReadonly;
}
public int Timeout
{
get { return pTimeout; }
set
{
if (value <= 0)
throw new ArgumentException("Timeout value must be greater than zero.");
if (value > MAX_TIMEOUT)
throw new ArgumentException("Timout cannot be greater than " + MAX_TIMEOUT.ToString());
pTimeout = value;
}
}
public string SessionID
{
get { return pId; }
}
public bool IsNewSession
{
get { return pNewSession; }
}
public SessionStateMode Mode
{
get { return pMode; }
}
public bool IsCookieless
{
get { return CookieMode == HttpCookieMode.UseUri; }
}
public HttpCookieMode CookieMode
{
get { return pCookieMode; }
}
//
// Abandon marks the session as abandoned. The IsAbandoned property is used by the
// session state module to perform the abandon work during the ReleaseRequestState event.
//
public void Abandon()
{
pAbandon = true;
}
public bool IsAbandoned
{
get { return pAbandon; }
}
//
// Session.LCID exists only to support legacy ASP compatibility. ASP.NET developers should use
// Page.LCID instead.
//
public int LCID
{
get { return Thread.CurrentThread.CurrentCulture.LCID; }
set { Thread.CurrentThread.CurrentCulture = CultureInfo.ReadOnly(new CultureInfo(value)); }
}
//
// Session.CodePage exists only to support legacy ASP compatibility. ASP.NET developers should use
// Response.ContentEncoding instead.
//
public int CodePage
{
get
{
if (HttpContext.Current != null)
return HttpContext.Current.Response.ContentEncoding.CodePage;
else
return Encoding.Default.CodePage;
}
set
{
if (HttpContext.Current != null)
HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding(value);
}
}
public HttpStaticObjectsCollection StaticObjects
{
get { return pStaticObjects; }
}
public object this[string name]
{
get { return pSessionItems[name]; }
set { pSessionItems[name] = value; }
}
public object this[int index]
{
get { return pSessionItems[index]; }
set { pSessionItems[index] = value; }
}
public void Add(string name, object value)
{
pSessionItems[name] = value;
}
public void Remove(string name)
{
pSessionItems.Remove(name);
}
public void RemoveAt(int index)
{
pSessionItems.RemoveAt(index);
}
public void Clear()
{
pSessionItems.Clear();
}
public void RemoveAll()
{
Clear();
}
public int Count
{
get { return pSessionItems.Count; }
}
public NameObjectCollectionBase.KeysCollection Keys
{
get { return pSessionItems.Keys; }
}
public IEnumerator GetEnumerator()
{
return pSessionItems.GetEnumerator();
}
public void CopyTo(Array items, int index)
{
foreach (object o in items)
items.SetValue(o, index++);
}
public object SyncRoot
{
get { return this; }
}
public bool IsReadOnly
{
get { return pIsReadonly; }
}
public bool IsSynchronized
{
get { return false; }
}
}
}
這樣我們在進行單元測試時就可以Mock掉Web下的Session,Cookie等對象。
例如:
[TestMethod()]
public void TestGetUserId()
{
TestHttpContext mock = new TestHttpContext(false);
System.Web.HttpContext context = mock.Context;
context.Session["UserId"] = 1245;
Assert.AreEqual(long.Parse(context.Session["UserId"].ToString()), 1245, "讀取用戶ID錯誤!");
}
原文轉自:http://www.anti-gravitydesign.com