0

Is there a way to change InstalledUICulture from English to Germany in test?

var groups = new ObservableDictionary<string, IGroupViewModel>(vm => 
    vm.Name.ToString(CultureInfo.InstalledUICulture));
var isGroupExists = groups.ContainsKey(GroupName.ToString(CultureInfo.InstalledUICulture));
if (!isGroupExists)
{
     groups.Add(new GroupViewModel());
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Probably not as a) the property is [get only](http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.installeduiculture.aspx) and b) it is documented as _"the equivalent of GetSystemDefaultUILanguage in the Windows API."_. If you want to test functionality under different cultures, then don't access the property directly, instead use indirection so you can return what you want to test, something like an `InstalledUiCultureProvider` class. – stuartd Nov 07 '17 at 15:04
  • Can you please give me some example? – Antony Nelson Nov 09 '17 at 14:08
  • Show how you're currently using `InstalledUICulture` – stuartd Nov 09 '17 at 14:13
  • `var groups = new ObservableDictionary(vm => vm.Name.ToString(CultureInfo.InstalledUICulture)); var isGroupExists = groups.ContainsKey(GroupName.ToString(CultureInfo.InstalledUICulture)); if (!isGroupExists) { groups.Add(new GroupViewModel()); }` – Antony Nelson Nov 10 '17 at 05:45

1 Answers1

2

Can you please give me some example?

Something like this. Instead of coupling your code directly to CultureInfo.InstalledUICulture you use a layer of indirection to allow you to pretend that the current UI culture is whatever you want.

I don't know your classes or how you are creating services, but I've assumed some sort of DI and used constructor parameters for the provider (or service or whatever you want to call it)

// App code
public interface ICurrentCultureProvider {
    CultureInfo CurrentInstalledUICulture { get; }
}

public class CurrentCultureProvider : ICurrentCultureProvider {
    public CultureInfo CurrentInstalledUICulture => CultureInfo.InstalledUICulture;
}

public class SUT {
    private readonly ICurrentCultureProvider cultureProvider;

    public SUT(ICurrentCultureProvider cultureProvider) {
        this.cultureProvider = cultureProvider;
    }

    public object Method(??? vm) {
        var currentUICulture = cultureProvider.CurrentInstalledUICulture;
        var groups = new ObservableDictionary<string, IGroupViewModel>(vm => vm.Name.ToString(currentUICulture));
        var isGroupExists = groups.ContainsKey(GroupName.ToString(currentUICulture));

        if (!isGroupExists) {
            groups.Add(new GroupViewModel());
        }

        return groups;
    }
}

// Test code. Could make this return any culture by using a parameter.
public class GermanCultureProvider : ICurrentCultureProvider {
    public CultureInfo CurrentInstalledUICulture => new CultureInfo("de-DE");
}

[TestFixture]
public class Sample_Test {

    [Test]
    public void Demo() {
        var sut = new SUT(new GermanCultureProvider());
        var vm = new ???();
        var groups = sut.Method(vm);
        // Assert correct in German etc
    }
}
stuartd
  • 70,509
  • 14
  • 132
  • 163