[英]How can I fake HttpContext for unit tests?
I need to fake HttpContext.Current.Application
table in order to access it from my unit tests. 我需要伪造HttpContext.Current.Application
表,以便从我的单元测试中访问它。
I need to store my data somewhere. 我需要将我的数据存储在某个地方。 I thought that I can just pass instance of NameValueCollectionBase
but as I discovered this base type has no indexer so it seems too complicated to use. 我认为我可以只传递NameValueCollectionBase
实例但是因为我发现这个基类型没有索引器所以它看起来太复杂了。
So what about faking this part of HttpContext
? 那么伪装这部分HttpContext
怎么样? Is it possible? 可能吗? How can I make it? 我该怎么做? Will be NUnit.Mocks
helpful? 会对NUnit.Mocks
有帮助吗?
Thank you in advance... 先感谢您...
Please go through below links it will help you. 请通过以下链接来帮助您。
http://www.java2s.com/Open-Source/CSharp/Web-Frameworks/MvcContrib/MvcContrib/TestHelper/Fakes/FakeHttpContext.cs.htm http://www.java2s.com/Open-Source/CSharp/Web-Frameworks/MvcContrib/MvcContrib/TestHelper/Fakes/FakeHttpContext.cs.htm
Mocking and HttpContextBase.get_User() 模拟和HttpContextBase.get_User()
Thanks Venkat 谢谢Venkat
If you need indexes for namevaluecollection base please use below code 如果您需要namevaluecollection base的索引,请使用以下代码
public static IEnumerable<KeyValuePair<string, string>> ToPairs(this NameValueCollection collection)
{
if(collection == null)
{
throw new ArgumentNullException("collection");
}
return collection.Cast<string>().Select(key => new KeyValuePair<string, string>(key, collection[key]));
}
For just to store data and passing around test methods please use above code. 如需存储数据和传递测试方法,请使用上面的代码。
In this scenario I generate some stubs derived from the base classes in System.Web.Abstractions. 在这种情况下,我生成一些从System.Web.Abstractions中的基类派生的存根。 I often use this technique for MVC applications as MVC / WebApi controllers contain an abstraction to HttpContext (HttpContextBase) 我经常将此技术用于MVC应用程序,因为MVC / WebApi控制器包含对HttpContext(HttpContextBase)的抽象
This way I can stub out HttpContext requirements in my unit / integration tests, here's a sample... 这样我可以在我的单元/集成测试中存根HttpContext要求,这是一个示例......
public class MockHttpApplicationState : HttpApplicationStateBase
{
private IDictionary<string, object> _appState = new Dictionary<string, object>();
public override void Add(string name, object value)
{
_appState.Add(name, value);
}
public override object Get(string name)
{
return _appState[name];
}
public override object this[string name]
{
get
{
return _appState[name];
}
set
{
_appState[name] = value;
}
}
}
public class MockHttpContext : HttpContextBase
{
private IDictionary<string, object> _appKeys;
public MockHttpContext()
{
}
/// <summary>
/// Accepts a dictionary of app keys to supply to the HttpApplicationState instance
/// </summary>
/// <param name="applicationState"></param>
public MockHttpContext(IDictionary<string,object> applicationState)
{
_appKeys = applicationState;
}
public override Cache Cache
{
get
{
return HttpRuntime.Cache;
}
}
public override HttpApplicationStateBase Application
{
get
{
var mockAppState = new MockHttpApplicationState();
foreach (string key in _appKeys.Keys)
{
mockAppState.Add(key, _appKeys[key]);
}
return mockAppState;
}
}
public override HttpRequestBase Request
{
get
{
return new HttpRequestWrapper(new HttpRequest(null,"http://localhost",null));
}
}
}
Then my test can establish Controller and Http Context: 然后我的测试可以建立Controller和Http Context:
private readonly OnlineShop.MVC.Controllers.HomeController _controller =
new MVC.Controllers.HomeController(null,new UnitOfWork());
[OneTimeSetUp]
public void Init()
{
var appKeys = new Dictionary<string, object>();
appKeys.Add("localhost", 1);
var httpContext = new MockHttpContext(appKeys);
_controller.ControllerContext = new ControllerContext()
{
Controller = _controller,
RequestContext = new RequestContext(httpContext, new RouteData())
};
}
[Test]
public void Index_Returns_HomeView()
{
var view = _controller.Index() as ViewResult;
var viewModel = view.Model as MVC.ViewModels.Home;
Assert.IsInstanceOf<OnlineShop.MVC.ViewModels.Home>(viewModel);
Assert.IsTrue(viewModel.FeaturedProducts.Count > 0);
}
And my controller is aware of it's ambient HttpContextBase instance supplying Cache and Application state: 我的控制器知道它提供缓存和应用程序状态的环境HttpContextBase实例:
public ActionResult Index()
{
string cacheKey = string.Format("FeaturedProducts-{0}",WebsiteId);
IList<Product> productList = this.HttpContext.Cache[cacheKey] as IList<Product>;
//My app keeps a list of website contexts in the Application. This test returns 1 based on the unit / int tests or a real world db value when hosted on IIS etc..
int websiteId = (int)HttpContext.Application[this.Request.Url.Host];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.