當我面對這類問題時(有問題的類沒有實現一個接口),我經常最終在有問題的類的周圍編寫一個包含相關接口的包裝器。然後我在我的代碼中使用我的包裝器。對於單元測試,我手動模擬包裝並插入我自己的模擬對象。
當然,如果一個模擬框架有效,那就用它來代替。我的經驗是所有的嘲笑框架都有各種.NET類的問題。
public interface ICacheWrapper
{
...methods to support
}
public class CacheWrapper : ICacheWrapper
{
private System.Web.Caching.Cache cache;
public CacheWrapper(System.Web.Caching.Cache cache)
{
this.cache = cache;
}
... implement methods using cache ...
}
public class MockCacheWrapper : ICacheWrapper
{
private MockCache cache;
public MockCacheWrapper(MockCache cache)
{
this.cache = cache;
}
... implement methods using mock cache...
}
public class MockCache
{
... implement ways to set mock values and retrieve them...
}
[Test]
public void CachingTest()
{
... set up omitted...
ICacheWrapper wrapper = new MockCacheWrapper(new MockCache());
CacheManager manager = new CacheManager(wrapper);
manager.Insert(item,value);
Assert.AreEqual(value, manager[item]);
}
真正的代碼
...
CacheManager manager = new CacheManager(new CacheWrapper(HttpContext.Current.Cache));
manager.Add(item,value);
...
你能否在問題中糾正你的拼寫問題,以便人們可以更容易地找到它? Cachcing =緩存 (不是試圖挑選你或任何東西 - 只是想幫助) – Chris 2009-11-13 04:25:06