If I try to unit test a view having Html.AntiForgeryToken() call inside I get an argument exception (An HttpContext is required to perform this operation. Check that this operation is being performed during a web request).
I tried to mock HttpContext (view.Context = Substitute.For<HttpContextBase>()) but is does not help.
(BTW: I am using MVC 5.0 and .NET 4.5)
Comments: An HttpContext is required to perform this operation. Check that this operation is being performed during a web request. at System.Web.Helpers.AntiForgery.GetHtml() at System.Web.Mvc.HtmlHelper.AntiForgeryToken() at WebApplication6.Views.Account.Login.Execute() in c:\Users\User\Documents\Visual Studio 2013\Projects\WebApplication6\WebApplication6\Views\Account\Login.cshtml:line 13 at RazorGenerator.Testing.WebViewPageExtensions.Render[TModel](WebViewPage`1 view, HttpContextBase httpContext, TModel model) at RazorGenerator.Testing.WebViewPageExtensions.RenderAsHtml[TModel](WebViewPage`1 view, HttpContextBase httpContext, TModel model) at RazorGenerator.Testing.WebViewPageExtensions.RenderAsHtml[TModel](WebViewPage`1 view, TModel model) at WebApplication6.Tests.Controllers.HomeControllerTest.Index() in c:\Users\User\Documents\Visual Studio 2013\Projects\WebApplication6\WebApplication6.Tests\Controllers\HomeControllerTest.cs:line 24
I tried to mock HttpContext (view.Context = Substitute.For<HttpContextBase>()) but is does not help.
(BTW: I am using MVC 5.0 and .NET 4.5)
Comments: An HttpContext is required to perform this operation. Check that this operation is being performed during a web request. at System.Web.Helpers.AntiForgery.GetHtml() at System.Web.Mvc.HtmlHelper.AntiForgeryToken() at WebApplication6.Views.Account.Login.Execute() in c:\Users\User\Documents\Visual Studio 2013\Projects\WebApplication6\WebApplication6\Views\Account\Login.cshtml:line 13 at RazorGenerator.Testing.WebViewPageExtensions.Render[TModel](WebViewPage`1 view, HttpContextBase httpContext, TModel model) at RazorGenerator.Testing.WebViewPageExtensions.RenderAsHtml[TModel](WebViewPage`1 view, HttpContextBase httpContext, TModel model) at RazorGenerator.Testing.WebViewPageExtensions.RenderAsHtml[TModel](WebViewPage`1 view, TModel model) at WebApplication6.Tests.Controllers.HomeControllerTest.Index() in c:\Users\User\Documents\Visual Studio 2013\Projects\WebApplication6\WebApplication6.Tests\Controllers\HomeControllerTest.cs:line 24