单元测试HttpContext.Current.Cache或C#等服务器端的方法呢? [英] Unit test HttpContext.Current.Cache or other server-side methods in C#?

查看:378
本文介绍了单元测试HttpContext.Current.Cache或C#等服务器端的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建一个类一个单元测试,使用 HttpContext.Current.Cache ,我使用NUnit时得到一个错误。该功能是基本 - 检查如果一个项目是在缓存中,如果没有,创建它并把它放在:

When creating a unit test for a class that uses the HttpContext.Current.Cache class, I get an error when using NUnit. The functionality is basic - check if an item is in the cache, and if not, create it and put it in:

if (HttpContext.Current.Cache["Some_Key"] == null) {
    myObject = new Object();
    HttpContext.Current.Cache.Insert("Some_Key", myObject);
}
else {
    myObject = HttpContext.Current.Cache.Get("Some_Key");
}

当从单元测试调用此,它无法与的NullReferenceException 缓存线的时候>。在Java中,我会使用仙人掌测试服务器端代码。是否有一个类似的工具,我可以使用C#代码? 这太问题提到模拟框架 - 这是我可以测试这些方法的唯一途径?是否有一个类似的工具来运行的C#测试?

When calling this from a unit test, it fails with at NullReferenceException when encountering the first Cache line. In Java, I would use Cactus to test server-side code. Is there a similar tool I can use for C# code? This SO question mentions mock frameworks - is this the only way I can test these methods? Is there a similar tool to run tests for C#?

另外,我不检查缓存是null作为我不想专门编写代码进行单元测试,假设一个服务器上运行时,它会一直有效。这是有效的,或者我应该添加缓存周围的空检查?

Also, I don't check if the Cache is null as I don't want to write code specifically for the unit test and assume it will always be valid when running on a server. Is this valid, or should I add null checks around the cache?

推荐答案

要做到这一点的方法是避免直接使用HttpContext的或其他类似的课程,并与嘲笑替换。毕竟,你是不是想测试HttpContext的正常运行(这是微软的工作),你只是想测试方法得到调用时,他们应该有。

The way to do this is to avoid direct use of the HttpContext or other similar classes, and substitute them with mocks. After all, you're not trying to test that the HttpContext functions properly (that's microsoft's job), you're just trying to test that the methods got called when they should have.

步骤(如果你只是想知道该技术无需通过博客负荷挖):

Steps (In case you just want to know the technique without digging through loads of blogs):


  1. 创建界面它描述了要在缓存来使用的方法(可能喜欢的东西的GetItem,SetItem,ExpireItem)。说它ICACHE或任何你喜欢

  1. Create an interface which describes the methods you want to use in your caching (probably things like GetItem, SetItem, ExpireItem). Call it ICache or whatever you like

创建一个实现该接口,并通过对实际的HttpContext

Create a class which implements that interface, and passes methods through to the real HttpContext

创建一个实现了相同的接口,公正的行为像一个模拟高速缓存的类。它可以使用字典或东西,如果你关心保存对象

Create a class which implements the same interface, and just acts like a mock cache. It can use a Dictionary or something if you care about saving objects

所以它不使用的HttpContext可言,而是永远只能改变你原来的代码使用一个ICACHE。然后,代码将需要获得ICACHE的一个实例 - 你可以在你的类的构造函数传递一个实例(这是所有依赖注入真的),或者把它贴在一些全局变量

Change your original code so it doesn't use the HttpContext at all, and instead only ever uses an ICache. The code will then need to get an instance of the ICache - you can either pass an instance in your classes constructor (this is all that dependency injection really is), or stick it in some global variable.

在您的生产应用程序,设置ICACHE成为您真正的HttpContext-已备份的高速缓存,并在单元测试中,设置ICACHE是模拟缓存。

In your production app, set the ICache to be your real HttpContext-Backed-Cache, and in your unit tests, set the ICache to be the mock cache.

利润!

这篇关于单元测试HttpContext.Current.Cache或C#等服务器端的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆