vendredi 30 octobre 2015

Unit testing with HttpContext.Application

I'm trying to unit test some code that reads a value from HttpContext.Current.Application but it keeps failing because it can't retrieve the value.

I've tried creating my own HttpContext, setting HttpContext.Current to it, then writing values to it but it does not seem to store the new values.

Code Referencing the HttpContext.Current.Application

public static void UpdateApplicationVariable(string keyToUpdate, object toSave)
{
    HttpContext.Current.Application.Lock();
    HttpContext.Current.Application[keyToUpdate] = toSave;
    HttpContext.Current.Application.UnLock();
}

public static object GetApplicationVariable(string keyToReturn)
{
    return HttpContext.Current.Application[keyToReturn];
}

Setup Code

HttpContext.Current = new HttpContext(
    new HttpRequest(null, "http://tempuri.org", null),
    new HttpResponse(null)
);

UpdateApplicationVariable("GeneralSettings", new GeneralSettings()
{
    NumberDecimalPlaces = 2
});

//settings is null
GeneralSettings settings = GetApplicationVariable("GeneralSettings") as GeneralSettings;

Aucun commentaire:

Enregistrer un commentaire