cultureinfo


How do I execute an MSpec specification in a specific Thread Culture?


I have a few specifications that test code performing culture-aware conversions. I want to set a defined culture for my tests so that I can hard-code the expected values without having to worry about the configured culture of the system running the tests.
Is there a simple way to do this with Machine.Specifications or do I have to set Thread.CurrentThread.CurrentCulture (and possibly CurrentUICulture as well)?
MSpec doesn't have any built-in facilities for changing the Thread's culture. However, it has several general methods for doing "something" before and after a test.
The "easy" way is to just use the Establish and Cleanup delegates.
[Subject("Culture Sensitive Tests")]
public class When_doing_culture_sensitive_stuff
{
Establish context = () =>
{
OldCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = NewCulture;
Thread.CurrentThread.CurrentUICulture = NewCulture;
}
Cleanup cleanup = () =>
{
Thread.CurrentThread.CurrentCulture = OldCulture;
Thread.CurrentThread.CurrentUICulture = OldCulture;
}
Because of = () => Subject.DoSomethingCultureSensitive();
It should_do_something_culture_sensitive = () => ...;
private static CultureInfo OldCulture;
private static CultureInfo NewCulture;
}
But, you'd need to share that in every test that requires it. So, I recommend a helper class that does the switching.
public class ChangeCurrentCulture : IDisposable
{
private readonly CultureInfo original;
public ChangeCurrentCulture(CultureInfo culture)
{
original = Thread.CurrentThread.CurrentCulture;
Change(culture)
}
private void Change(CultureInfo culture)
{
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
public void Dispose()
{
Change(original);
}
}
And you can setup the call in a shared base class. The delegates get chained when you run the specs.
public abstract class CultureSensitive
{
Establish context = () =>
{
Change = new ChangeCurrentCulture(NewCulture);
}
Cleanup cleanup = () =>
{
Change.Dispose();
}
private static ChangeCurrentCulture Change;
private static CultureInfo NewCulture;
}
[Subject("Culture Sensitive Tests")]
public class When_doing_culture_sensitive_stuff : CultureSensitive
{
Because of = () => Subject.DoSomethingCultureSensitive();
It should_do_something_culture_sensitive = () => ...;
}
Another option depends on you separating all of the culture sensitive tests into a separate assembly. The IAssemblyContext interface gives you two assembly-wide setup and cleanup methods. You could change the culture for all the specs in that assembly (don't worry about cleanup).
public class CultureSensitiveTests : IAssemblyContext
{
public void OnAssemblyStart()
{
Thread.CurrentThread.CurrentCulture = NewCulture;
Thread.CurrentThread.CurrentUICulture = NewCulture;
}
public void OnAssemblyComplete()
{
}
}

Related Links

What does “the string representation returned by ToString is required to take into account the current system culture” means?
Culture not found exception, new CultureInfo(“en-EN”)
How do I execute an MSpec specification in a specific Thread Culture?
Sitefinity 5.3 displaying Culture info at the end of URL
Where is CultureInfo safed in assembly?
Refactoring two basic classes

Categories

HOME
ibm-bluemix
winforms
debugging
gremlin
session
grep
bookshelf.js
paw-app
win32gui
installshield
android-youtube-api
dacpac
spring-xd
numeral.js
spring-tool-suite
n-gram
ibm-odm
windows-server-2012
percona
jsprit
caml
undefined
connection-string
oracle-coherence
visual-composer
emulator
immutable.js
jasonette
google-search-api
autosys
javacv
sparse-matrix
kannel
kendo-datasource
file-rename
r-raster
nameservers
pim
form-data
atl
mmenu
main
restlet
elasticsearch-plugin
avro4s
firebase-admin
total-commander
executenonquery
scorm
react-scripts
multipeer-connectivity
powercli
mu
mplayer
slickedit
lift-json
sqlbulkcopy
flutterwave
redundancy
sonarlint-vs
yt-project
phishing
pcf
etsy
whois
livequery
t4mvc
metaclass
angular-leaflet-directive
vstest.console.exe
processmodel
sniffer
c++03
network-interface
document-classification
jubula
wp-query
viadeo
ccss
sitemesh
jquery-mobile-dialog
random-seed
nsnetservice
gdata-api
dice
netdna-api
self-extracting
pyinotify
mongomapper
google-friend-connect
.nettiers
sef
virtual-functions
privilege
defensive-programming

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App