Webクラスライブラリコードのユニットテスト

5696 ワード

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
{
    /// 
    /// system.web.caching   
    ///   web
    /// 
    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];
        }
        /// 
        ///       cache 6 hours
        /// 
        ///   
        ///  ,    
        public static void AddCache(string key, object obj)
        {
            MyCache.Add(key, obj, null, DateTime.Now.AddHours(6), TimeSpan.Zero, CacheItemPriority.High, null);
        }
        /// 
        ///       cache,    
        /// 
        /// 
        /// 
        /// 
        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";
        /// 
        ///   setup,    async
        /// 
        [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);
        }
        /// 
        ///     setup,    async
        /// 
        [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", "/");  //          
            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にしないでください.