1

I've created an assembly MyResources with two resx:

  • MyResources.resx
  • MyResources.en.resx

Inside the assembly I've added a handler-class containing a GetString-wrapper inside a ResHandler-class:

    public string GetResString(string key)
    {            
        return _manager.GetString(key, _culture);
    }

_culture is simply a property which can be set from outside:

    public void ChangeCulture(CultureInfo newCulture)
    {            
        _culture = newCulture;
    }

If I call this code from a lets say console-app, everything works fine:

    var res = ResHandler.GetInstance(Guid.NewGuid().ToString());
    //change the culture to "en"
    res.ChangeCulture(new CultureInfo("en"));
    Console.WriteLine(res.GetResString("TXT_0001"));

This code writes the english version to the console. However, if I call the exact same code from a unit-test-method, the contents of the MyResources.resx will appear. Whats wrong here? Are unit-tests unable to do this for some reason?

Alexander Schmidt
  • 5,631
  • 4
  • 39
  • 79
  • No. I mean it always falls back to the CurrentThread-Culture in unit-test-mode. What do you mean with 'disappear' :-)? – Alexander Schmidt Aug 14 '11 at 11:54
  • 1
    You said that "content of the MyResources.resx will appear" but what is expected? BTW, how ResHandler.ChangeCUlture() is implemented, where is it set culture passed as parameter? – sll Aug 14 '11 at 12:14
  • Ok, i edited the post. With "appear" I ment to say, that the correkt string out of the resource is written to the console. Sorry, sometimes I'm translating wrong from german to english ;-). – Alexander Schmidt Aug 14 '11 at 12:24
  • Have you tried to set a break point around all places where value is assigned to _culture? Also set a breakpoint on the line _manager.GetString(key, _culture); and see perhaps null value is passed so in this case this could cause some kind of default or unexpected behaviour – sll Aug 14 '11 at 12:31
  • Yes, I already have done this. The assignment inside of ChangeUICulture is made correctly. No error appears at all. Remember: If called from Console-App, everything works correctly without any code-change! – Alexander Schmidt Aug 14 '11 at 12:35

1 Answers1

1

Beware that satellite assemblies are stored in a subdirectory of the directory that contains the EXE. Like "en-US" or "en" for English. Problem is, your test runs under a different EXE, mstest.exe and not your app.exe. It will therefore not find the satellite assembly. I think you can fix this by using Deployment in the test settings, not sure.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I guess, this is the right way. Thanks for that, but if I configure to deploy the "en"-folder there, it won't be copied to the TestResults\*\out-Folder. Any ideas what is needed else? – Alexander Schmidt Aug 14 '11 at 14:40