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: Hmmm, they have some pretty dumb code in https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.WebPages/Helpers/AntiForgery.cs: ``` public static HtmlString GetHtml() { if (HttpContext.Current == null) { throw new ArgumentException(WebPageResources.HttpContextUnavailable); } ... } ``` Not sure how we can possibly mock this! :(
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: Hmmm, they have some pretty dumb code in https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.WebPages/Helpers/AntiForgery.cs: ``` public static HtmlString GetHtml() { if (HttpContext.Current == null) { throw new ArgumentException(WebPageResources.HttpContextUnavailable); } ... } ``` Not sure how we can possibly mock this! :(