用于web类库代码的单元测试
来源:互联网 发布:关于开关电源的软件 编辑:程序博客网 时间:2024/06/06 09:37
用于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就可以
0 0
- 用于web类库代码的单元测试
- Spring Mock--用于Spring 的单元测试
- Spring-Mock--用于Spring 的单元测试
- Web Api 的单元测试
- Spring Web 单元测试实用HelloWorld(附代码)
- Spring web项目的单元测试
- 用于自省的代码段
- At.js – 用于 Web 应用程序的自动完成库
- 单元测试代码覆盖率的浅谈
- 做后缀数组类的题用于调试的代码
- 一个用于生成验证码的C#类代码
- 写了个单元测试辅助类,用于读取文本加入数据,删除数据和检查数据的
- springUtil 获取bean,用于单元测试
- 用于实现Web Services安全性的HTTPS
- 用于实现Web Services安全性的HTTPS
- 用于Web服务的核心Java API
- 用于WEB应用的Java技术简介
- 用于 J2ME 的 Web 服务 API
- 【详解】更新整个maven工程及相关底层并再运行的记录
- asp.net 母版页使用详解
- dos批处理:列出指定目录下所有文件
- Linux环境Tomcat运行报错java.lang.OutOfMemoryError
- spring security 转
- 用于web类库代码的单元测试
- Qt之认识JSON
- 串的模式匹配算法---RK
- 手机页面页脚代码
- [Java]Java工程师成神之路
- CPU affinity
- 有一个本地文件的路径或者文件的名字,计算文件的大小
- iOS---取出字符串中的数字
- java内存管理