今天我遇到了一個問題,我無法在通過私有方法訪問MS MS單元測試方法時在我的控制器中調用ControllerContext。例如MS單元測試訪問私有方法和基類成員
//This is my controller and private GetUsers() method
public class SampleController : Controller
{
private IEnumerable<Users> GetUsers()
{
try
{
string cacheKey = "UserKey";
IList<User> users;
if (this.HttpContext.Cache[cacheKey] != null)
{
users= (IList<User>)this.HttpContext.Cache[cacheKey];
}
else
{
users= UserService.GetUsers();
if (users!= null)
{
this.HttpContext.Cache.Insert(cacheKey, users, null, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration);
}
}
return UserExtensions.GetModifiedUsers(users);
}
catch (Exception ex)
{
throw ex;
}
}
}
//In Unit Tests
[TestMethod]
public void SampleTestMethod()
{
SampleController_Accessor privateAcc = new SampleController_Accessor();
privateAcc.ControllerContext //Which is not availble intelliSense ???????????
}
有沒有一種方法來訪問ControllerContext,而無需在單元測試方法中修改控制器?
我需要ControllerContext,所以我可以設置嘲笑的HttpContext爲控制器
我試圖
((SampleController)privateAcc).ControllerContext = this.GetControllerContext();
但是編譯器會引發錯誤。
任何想法非常感謝。