起订量:单元测试依托方法的HttpContext [英] Moq: unit testing a method relying on HttpContext

查看:117
本文介绍了起订量:单元测试依托方法的HttpContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET程序集考虑一个方法:

Consider a method in a .NET assembly:

public static string GetSecurityContextUserName()
{             
 //extract the username from request              
 string sUser = HttpContext.Current.User.Identity.Name;
 //everything after the domain     
 sUser = sUser.Substring(sUser.IndexOf("\\") + 1).ToLower();

 return sUser;      
}

我想打电话从使用起订量框架单元测试这种方法。该组件是一个web表单解决方案的一部分。单元测试看起来是这样,但我错过了起订量code。

I'd like to call this method from a unit test using the Moq framework. This assembly is part of a webforms solution. The unit test looks like this, but I am missing the Moq code.

//arrange 
 string ADAccount = "BUGSBUNNY";
 string fullADName = "LOONEYTUNES\BUGSBUNNY"; 

 //act    
 //need to mock up the HttpContext here somehow -- using Moq.
 string foundUserName = MyIdentityBL.GetSecurityContextUserName();

 //assert
 Assert.AreEqual(foundUserName, ADAccount, true, "Should have been the same User Identity.");


  • 如何使用起订量来安排像'MYDOMAIN \\ MYUSER'某个值假的HttpContext对象?

  • 如何关联,假冒我在 MyIdentityBL.GetSecurityContextUserName电话到我的静态方法()

  • 请您对如何改进本code /架构?
  • 任何建议
  • How can I use Moq to arrange a fake HttpContext object with some value like 'MyDomain\MyUser'?
  • How do I associate that fake with my call into my static method at MyIdentityBL.GetSecurityContextUserName()?
  • Do you have any suggestions on how to improve this code/architecture?

推荐答案

Web表单是这个确切原因出了名的不可测 - 很多code都可以在asp.net管道依靠静态类

Webforms is notoriously untestable for this exact reason - a lot of code can rely on static classes in the asp.net pipeline.

为了与起订量来测试这一点,你需要重构你的 GetSecurityContextUserName()方法使用依赖注入与 HttpContextBase 对象。

In order to test this with Moq, you need to refactor your GetSecurityContextUserName() method to use dependency injection with an HttpContextBase object.

HttpContextWrapper 位于 System.Web.Abstractions ,其中附带的.Net 3.5。这是的HttpContext 类的包装,并延伸 HttpContextBase ,你可以构造一个 HttpContextWrapper 就像这样:

HttpContextWrapper resides in System.Web.Abstractions, which ships with .Net 3.5. It is a wrapper for the HttpContext class, and extends HttpContextBase, and you can construct an HttpContextWrapper just like this:

var wrapper = new HttpContextWrapper(HttpContext.Current);

更妙的是,你可以模拟一个HttpContextBase和设置您的期望它使用起订量上。包括登录的用户,等等。

Even better, you can mock an HttpContextBase and set up your expectations on it using Moq. Including the logged in user, etc.

var mockContext = new Mock<HttpContextBase>();

有了这个地方,你可以叫 GetSecurityContextUserName(mockContext.Object),您的应用程序要少得多耦合到静态的WebForms的HttpContext。如果你打算做了很多的依靠嘲笑方面的测试,我强烈建议考虑看看在斯科特Hanselman的MvcMockHelpers类,它具有与起订量使用的版本。它方便地处理大量需要设置的。尽管名字,你并不需要使用MVC做了它 - 我成功地用它与web表单的应用程序时,我可以重构他们使用 HttpContextBase

With this in place, you can call GetSecurityContextUserName(mockContext.Object), and your application is much less coupled to the static WebForms HttpContext. If you're going to be doing a lot of tests that rely on a mocked context, I highly suggest taking a look at Scott Hanselman's MvcMockHelpers class, which has a version for use with Moq. It conveniently handles a lot of the setup necessary. And despite the name, you don't need to be doing it with MVC - I use it successfully with webforms apps when I can refactor them to use HttpContextBase.

这篇关于起订量:单元测试依托方法的HttpContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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