考虑一个方法在.NET组件:起定量:单元测试依靠HttpContext的方法
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;
}
我想打电话从使用Moq的框架单元测试此方法。这个程序集是webforms解决方案的一部分。单元测试看起来像这样,但我缺少Moq代码。
//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()
有我的电话,假冒联想到我的静态方法? - 你有关于如何提高这个代码/架构有什么建议?
不能使用新HttpContextBase(),因为它是一个抽象类(顾名思义)。您需要使用新的HttpContextWrapper()。 – 2009-11-19 17:53:23
谢谢......当我回答这个问题时,我的脑袋超出了我的预期;) – womp 2009-11-19 20:12:09