开发者

Setting CultureInfo in Tests project?

开发者 https://www.devze.com 2022-12-18 07:56 出处:网络
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 ge开发者_StackOverflow社区tting an issue in my service layer. I\'m ask

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 ge开发者_StackOverflow社区tting 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?


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.


ConfigurationSettings.AppSettings["CultureToUse"]

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

0

精彩评论

暂无评论...
验证码 换一张
取 消