views:

189

answers:

2

I've got an MVC app that I've set the globalization in the web.config. All is well in the web app. But in my tests project I'm getting an issue in my service layer. I'm asking for date of birth in the following format dd/MM/yyyy. I'm passing this as a string to my service layer. I've got a RegEx to check that it is formatted correctly but when it is and I try to convert it to a date I'm getting an error. This is because the CultureInfo is set to en.US, I want it to be en.GB. I've tried in one of my initialise test methods to do the following, to no avail:

string sCulture = ConfigurationSettings.AppSettings["CultureToUse"];  //returns "en.GB"
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(sCulture);
        CultureInfo.CreateSpecificCulture(sCulture);

Any ideas how to set CultureInfo in my tests project?

+1  A: 
ConfigurationSettings.AppSettings["CultureToUse"]

Must return "en-GB" not "en.GB", hope this helps!

Alex LE
yes it already does, sorry was a typo.
lloydphillips
+1  A: 

Since you enforce the format that the data is in and it only is numeric, you shouldn't rely on a user defined setting and are better off using DateTime.ParseExact(dateString, "dd/MM/yyyy", CultureInfo.InvariantCulture).

The CurrentUICulture property controls the resources that get loaded for the app. CurrentCulture is what you want to set/get to control parsing/formatting.

Eric MSFT
Did the job, thanks!
lloydphillips