This project is read-only.

AntiForgeryToken() html helper throws NotImplementedException

Sep 26, 2011 at 9:10 PM

Whenever I attempt to .RenderAsHtml() a RazorGenerator-generated view containing a call to the AntiForgeryToken() HtmlHelper, a NotImplementedException is thrown at

at System.Web.HttpRequestBase.get_Cookies() 

at System.Web.Helpers.AntiForgeryWorker.GetAntiForgeryTokenAndSetCookie(HttpContextBase httpContext, String salt, String domain, String path) 

at System.Web.Helpers.AntiForgeryWorker.GetHtml(HttpContextBase httpContext, String salt, String domain, String path)   

at System.Web.Helpers.AntiForgery.GetHtml(HttpContextBase httpContext, String salt, String domain, String path) 

at System.Web.Mvc.HtmlHelper.AntiForgeryToken(String salt, String domain, String path)   at System.Web.Mvc.HtmlHelper.AntiForgeryToken(String salt) 

at System.Web.Mvc.HtmlHelper.AntiForgeryToken()

In an attempt to fix this, I have modified my test as such:

	[Test]
        public void Index_RendersView()
        {
            var view = new Index();
            view.ViewContext = new ViewContext();
            view.ViewContext.HttpContext = FakeHttpContextCreator.FakeHttpContext();
            // Should not throw exception for test to pass:        
            view.RenderAsHtml();
        }
And the fake HttpContext's FakeHttpRequest implementation's .Cookies property getter returns a "new HttpCookieCollection()".  I've pulled down the latest RazorGenerator source and I can see that RenderAsHtml() invokes Render() which seems to properly pass my 
view's ViewContext.HttpContext property down into the WebPageContext() constructor, at which point my knowledge of how to dig any further ends.  

Has anyone encountered this and found a work-around?  
Thanks!

Sep 26, 2011 at 9:38 PM

Ok, I just checked in a fix for that (needed to mock a few more things). Can you build RazorGenerator.Testing yourself from the sources and verify? If it works well, I'll push a new nuget package.

Thanks for reporting this!

Sep 26, 2011 at 11:02 PM

I don't totally understand the interaction of the projects within the source code solution to figure out how to replace my test project's reference to RazorGenerator.Mvc with RazorGenerator.Testing, such that the WebViewPageExtensions class within the .Testing project gets invoked in place of the same class in the 

.Mvc project, but when I add a reference to the .Testing assembly which I just built freshly from your source and then change my test to reference the test method as follows:

        [Test]
        public void Index_RendersView()
        {
            var view = new Index();
            var model = new FileIndexViewModel();
            view.ViewContext = new ViewContext();
            view.ViewContext.HttpContext = FakeHttpContextCreator.FakeHttpContext();
            // Should not throw exception for test to pass:  

            RazorGenerator.Testing.WebViewPageExtensions.RenderAsHtml(view, model);
            //view.RenderAsHtml(model);
        }

. . . it works like a champ.

Thanks for your help with this!  I've already seen a considerable benefit from using this tool that you have created, and now with this issue fixed, I can create tests against the rest of the forms in my project.

Sep 26, 2011 at 11:18 PM

Great! I just pushed RazorGenerator.Testing 1.2.2 to NuGet, so you should be able to use that instead of your private build now.