Model view controller MS单元测试访问私有方法和基类成员
今天我遇到了一个问题,当通过私有方法访问时,我无法在MS单元测试方法中调用控制器中的ControllerContext。比如说Model view controller MS单元测试访问私有方法和基类成员,model-view-controller,unit-testing,controller,mstest,Model View Controller,Unit Testing,Controller,Mstest,今天我遇到了一个问题,当通过私有方法访问时,我无法在MS单元测试方法中调用控制器中的ControllerContext。比如说 //This is my controller and private GetUsers() method public class SampleController : Controller { private IEnumerable<Users> GetUsers() { try
//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 ???????????
}
但编译器抛出了一个错误
非常感谢任何想法。您可以使用该属性
下面是一个示例:这段代码不是真正的单元可测试的-太多依赖于静态数据。编写一个集成测试,一天一次,或者把它分成两个类——一个类获取静态内容,另一个类执行任何必要的转换 您可能可以使用像TypeMock这样的“强”模拟框架,但我不喜欢它们
((SampleController)privateAcc).ControllerContext = this.GetControllerContext();