Moq: модульное тестирование метода, основанного на HttpContext

Рассмотрим метод в сборке .NET:

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.");

Question:

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?

Ответы на вопрос(6)

Ваш ответ на вопрос