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

查看:12
本文介绍了单元测试 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");
}

当从单元测试中调用它时,遇到第一行 Cache 时它会在 NullReferenceException 处失败.在 Java 中,我会使用 Cactus 来测试服务器端代码.有没有类似的工具可以用于 C# 代码?这个 SO question 提到了模拟框架- 这是我测试这些方法的唯一方法吗?是否有类似的工具可以为 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#?

另外,我不检查 Cache 是否为空,因为我不想专门为单元测试编写代码,并假设它在服务器上运行时始终有效.这是有效的,还是我应该在缓存周围添加空检查?

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 或其他类似的类,并用 mock 代替它们.毕竟,您不是在尝试测试 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-Backed-Cache,并在您的单元测试中,将 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天全站免登陆