Moq: jednostka testująca metodę polegającą na HttpContext

Rozważ metodę w zespole .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;      
}

Chciałbym wywołać tę metodę z testu jednostkowego przy użyciu struktury Moq. Ten zespół jest częścią rozwiązania formularzy internetowych. Test jednostkowy wygląda tak, ale brakuje mi kodu 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.");

Pytanie:

Jak mogę użyć Moq do aranżacji fałszywego obiektu HttpContext z pewną wartością taką jak „MyDomain MyUser”?Jak skojarzyć ten fałszywy z moim wezwaniem do mojej metody statycznej wMyIdentityBL.GetSecurityContextUserName()?Czy masz jakieś sugestie dotyczące ulepszenia tego kodu / architektury?

questionAnswers(6)

yourAnswerToTheQuestion