ResourceManager.GetString 在单元测试中失败 [英] ResourceManager.GetString fails in unit tests
问题描述
我用两个 resx 创建了一个程序集 MyResources:
I've created an assembly MyResources with two resx:
- MyResources.resx
- MyResources.en.resx
在程序集中,我添加了一个处理程序类,其中包含一个 ResHandler 类中的 GetString 包装器:
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 只是一个可以从外部设置的属性:
_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"));
此代码将英文版本写入控制台.但是,如果我从单元测试方法中调用完全相同的代码,则会出现 MyResources.resx 的内容.这里有什么问题?单元测试是否由于某种原因无法做到这一点?
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?
推荐答案
请注意附属程序集存储在包含 EXE 的目录的子目录中.就像英语的en-US"或en".问题是,您的测试在不同的 EXE 下运行,mstest.exe 而不是您的 app.exe.因此,它不会找到附属程序集.我认为您可以通过在测试设置中使用 Deployment 来解决这个问题,不确定.
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.
这篇关于ResourceManager.GetString 在单元测试中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!