用于web类库代码的单元测试
最近把appcache的功能放到了公用类库中,写单元测试时发现用不了 HttpContext,
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;
namespace Lib.Csharp.Tools
{
/// <summary>
/// system.web.caching操作类
/// 用于web
/// </summary>
public class AppCache
{
public static Cache MyCache = HttpContext.Current.Cache;
private AppCache() { }
public static bool IsExist(string key)
{
//MyCache = HttpContext.Current.Cache;
if (MyCache[key] != null)
{
return true;
}
else
{
return false;
}
}
public static void Add(string key, object obj)
{
MyCache.Add(key, obj, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}
public static void Add(string key, object obj, string file)
{
MyCache.Add(key, obj, new CacheDependency(file), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}
public static void Remove(string key)
{
MyCache.Remove(key);
}
public static object Get(string key)
{
return MyCache[key];
}
/// <summary>
/// 加时间限制的cache 6 hours
/// </summary>
/// <param name="key">键值</param>
/// <param name="obj">值,对象类型</param>
public static void AddCache(string key, object obj)
{
MyCache.Add(key, obj, null, DateTime.Now.AddHours(6), TimeSpan.Zero, CacheItemPriority.High, null);
}
/// <summary>
/// 加时间限制的cache,单位小时
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="hours"></param>
public static void AddCache(string key, object obj, int hours)
{
MyCache.Add(key, obj, null, DateTime.Now.AddHours(hours), TimeSpan.Zero, CacheItemPriority.High, null);
}
}
}
由于
HttpContext 相关的属于web功能,nuint 测试框架本身不支持,后来查资料发现可以在测试中直接new 出 HttpContent,这样就可以进行单元测试了,
代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Hosting;
using Lib.Csharp.Tools;
using NUnit.Framework;
namespace Lib.Csharp.ToolsTests
{
[TestFixture()]
public class AppCacheTests
{
private string cacheKey = "Test-Key";
private string cacheValue = "Test-Value";
private string cacheKey2 = "Test-Key2";
private string cacheValue2 = "Test-Value2";
/// <summary>
/// 全局setup,不能使用async
/// </summary>
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
Thread.GetDomain().SetData(".appPath", "c:\\inetpub\\wwwroot\\webapp\\");
Thread.GetDomain().SetData(".appVPath", "/");
TextWriter tw = new StringWriter();
String address = "home.myspace.cn";
HttpWorkerRequest wr = new SimpleWorkerRequest("default.aspx", "friendId=1300000000", tw);
HttpContext.Current = new HttpContext(wr);
}
/// <summary>
/// 每次测试setup,不能使用async
/// </summary>
[SetUp]
public void SetUp()
{
AppCache.Remove(cacheKey);
}
[Test()]
public void IsExistTest()
{
var isHave = AppCache.IsExist(cacheKey);
Assert.IsFalse(isHave);
AppCache.AddCache(cacheKey, cacheValue);
isHave = AppCache.IsExist(cacheKey);
Assert.IsTrue(isHave);
}
[Test()]
public void AddTest()
{
AppCache.Add(cacheKey, cacheValue);
var result = AppCache.Get(cacheKey);
Assert.AreEqual(result.ToString(), cacheValue);
}
[Test()]
public void AddTest1()
{
AppCache.AddCache(cacheKey, cacheValue);
var result = AppCache.Get(cacheKey);
Assert.AreEqual(result.ToString(), cacheValue);
AppCache.AddCache(cacheKey2, cacheValue2);
var result2 = AppCache.Get(cacheKey2);
Assert.AreEqual(result2.ToString(), cacheValue2);
}
[Test()]
public void RemoveTest()
{
AppCache.AddCache(cacheKey, cacheValue);
var isHave = AppCache.IsExist(cacheKey);
Assert.IsTrue(isHave);
AppCache.Remove(cacheKey);
isHave = AppCache.IsExist(cacheKey);
Assert.IsFalse(isHave);
}
}
}
说明:
public void TestFixtureSetUp()
{
Thread.GetDomain().SetData(".appPath", "c:\\inetpub\\wwwroot\\webapp\\"); //这个设置里的值不重要
Thread.GetDomain().SetData(".appVPath", "/"); //这个设置里的值不重要</span><span style="white-space:pre">
</span> TextWriter tw = new StringWriter();
String address = "home.myspace.cn";
HttpWorkerRequest wr = new SimpleWorkerRequest("default.aspx", "friendId=1300000000", tw); //这个设置里的值不重要
HttpContext.Current = new HttpContext(wr);
}
有了HttpContext后,测试就没问题了,不让 HttpContext为null就可以