You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
While trying to mock a controller I came across a KeyNotFoundException in the UrlHelperFactory's GetUrlHelper method.
It seems the following code relies on the typeof(IUrlHelper) key already populated in the Items collection:
// Perf: Create only one UrlHelper per contextvarurlHelper=httpContext.Items[typeof(IUrlHelper)]asIUrlHelper;if(urlHelper==null){urlHelper=newUrlHelper(context);httpContext.Items[typeof(IUrlHelper)]=urlHelper;}
If the key is missing, this line throws an exception, instead of returning null. I believe that the following code should be used instead:
// Perf: Create only one UrlHelper per contextobjecturlHelper;if(!httpContext.Items.TryGetvalue(typeof(IUrlHelper),outurlHelper)||!(urlHelperisIUrlHelper)){urlHelper=newUrlHelper(context);httpContext.Items[typeof(IUrlHelper)]=urlHelper;}return(IUrlHelper)urlHelper;
The text was updated successfully, but these errors were encountered:
Did you create a mock for HttpContext.Items? Right now this code expects Items to return null on a missing key, which is something we should probably fix since it's specified as IDictionary<>.
Regardless, I'd strongly suggest using DefaultHttpContext in your tests - you'll need less mock setup stuff. It's what we do and we haven't run into any limitations.
While trying to mock a controller I came across a
KeyNotFoundException
in theUrlHelperFactory
'sGetUrlHelper
method.It seems the following code relies on the
typeof(IUrlHelper)
key already populated in theItems
collection:If the key is missing, this line throws an exception, instead of returning
null
. I believe that the following code should be used instead:The text was updated successfully, but these errors were encountered: