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.
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.
So what about faking this part of HttpContext
? Is it possible? How can I make it? Will be开发者_StackOverflow中文版 NUnit.Mocks
helpful?
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
Mocking and HttpContextBase.get_User()
Thanks Venkat
If you need indexes for namevaluecollection base please use below code
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. I often use this technique for MVC applications as MVC / WebApi controllers contain an abstraction to HttpContext (HttpContextBase)
This way I can stub out HttpContext requirements in my unit / integration tests, here's a sample...
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:
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:
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];
精彩评论