gpt4 book ai didi

c# - 简单的 ResourceManager 示例在单元测试中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:37 25 4
gpt4 key购买 nike

试图遵循这个:http://msdn.microsoft.com/en-us/library/21a15yht.aspx该示例的问题在于它混合了针对 Visual Studio 用户和命令行用户的说明,但我已尽力仅遵循 VS 说明。

所以我有: 在 vi​​sual studio 中创建了一个名为 Example 的新单元测试项目.

我添加了一个名为 Greeting.en-US.resx 的资源并放入一个名为 HelloString 的字符串.

编辑 我现在添加了另一个名为 Greeting.resx 的默认资源文件字符串是“你好(默认)”

enter image description here

我的单元测试:

using System.Globalization;
using System.Resources;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Example
{
[TestClass]
public class GreetingTests
{
[TestMethod]
public void Hello()
{
var newCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;

var rm = new ResourceManager("Example.Greeting",
typeof (GreetingTests).Assembly);
Assert.AreEqual("Hello (US)", rm.GetString("HelloString"));
}
}
}

现在它只加载默认值

Assert.AreEqual failed. Expected:<Hello (US)>. Actual:<Hello (Default)>.

相同的代码在控制台应用程序中运行得很好。

最佳答案

好吧,我认为发生这种情况是因为您的单元测试工具影子复制了主程序集,但没有复制附属程序集。 Enable shadow copying for satellite assembly并重新运行单元测试。

为了确认我对 Assembly.GetExecutingAssembly().Location 与 .Codebase 的猜测检查值。

关于c# - 简单的 ResourceManager 示例在单元测试中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16792377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com