I am trying to create a wrapper class to handle the contents of an HttpContext. I am creating a cookie but cannot add it to the HttpContext.Request or Response files collection.
I am using Moq. I also use MvcMockHelp at the following link: http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx
When I try to add cookies to my collection in my following code:
HttpContextBase c1 = MvcMockHelpers.FakeHttpContext(); HttpCookie aCookie = new HttpCookie("userInfo"); aCookie.Values["userName"] = "Tom"; c1.Request.Cookies.Add(aCookie); <------ Error here
I get the following error in the 4th line of code c1.Request.Cookies.Add (aCookie);
Object reference not set to an instance of an object.
I also tried to instantiate the context object as follows, but still no luck
HttpContextBase c = MvcMockHelpers.FakeHttpContext ("~/script/directory/NAMES.ASP?city=irvine&state=ca&country=usa");
I see that the Cookies collection inside the request is NULL. How to create it?
I also tried the following, but no luck.
c1.Request.Cookies["userName"].Value = "Tom";
Please let me know what I am doing wrong.
dotnet practitioner
source share