If you look at the code for HttpCookieCollection.Get(string), you'll see something like this:
public HttpCookie Get(string name)
{
HttpCookie cookie = (HttpCookie) this.BaseGet(name);
if (cookie == null && this._response != null)
{
cookie = new HttpCookie(name);
this.AddCookie(cookie, true);
this._response.OnCookieAdd(cookie);
}
if (cookie != null)
this.EnsureKeyValidated(name, cookie.Value);
return cookie;
}
It's never creating the cookie because _response is going to be null (look at the first 'if' statement). i.e. there's no response object to send the new cookie back to, so it won't create it.
The response object is an HttpResponse object and it's passed through to the internal constructor (so that constructor is not available to you).
I personally never liked the way the Get method acts on HttpCookieCollection; it violates the Command-Query separation principle: asking a question should not change the answer.
I would recommend you check for the existence of the cookie by checking the AllKeys property; if it doesn't exist, explicitly create and add the cookie to the collection. Otherwise, if you know the key exists, go ahead and fetch the existing entry. Then your production code and unit tests should behave.
It might be a good idea to create a helper or extension method to use instead of Get, to ensure it behaves as you expect whether you're unit testing or running normally:
public static class HttpCookieCollectionExtensions
{
public static HttpCookie GetOrCreateCookie(this HttpCookieCollection collection, string name)
{
// Check if the key exists in the cookie collection. We check manually so that the Get
// method doesn't implicitly add the cookie for us if it's not found.
var keyExists = collection.AllKeys.Any(key => string.Equals(name, key, StringComparison.OrdinalIgnoreCase));
if (keyExists) return collection.Get(name);
// The cookie doesn't exist, so add it to the collection.
var cookie = new HttpCookie(name);
collection.Add(cookie);
return cookie;
}
}