Relative Content

Tag Archive for c#moqhttpcontext

How to properly mock HttpContextAccessor for controller testing in .Net 6

I have the following code for mocking the HttpContextAccessor. Most of it works fine but the area that I can’t get to work as expected is getting form values, specifically when trying to get them while iterating over the Keys collection. I put a breakpoint on the AddFormFieldsOrFile method where it creates the new requestForm and when I mouse over that, it shows me an object with properties like Count, Keys, and Store and it has the values I added to it.