开发者

ResourceManager.GetString fails in unit tests

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.GetInstan开发者_开发技巧ce(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?


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜