2

我正在尝试编写一个单元测试来访问我与项目关联的资源。这是一般结构。该资源具有打算由 MyLibClass 使用的字符串,并且该类设置为en-US

Solution1
--MyLibrary
  --Properties
     --AssemblyInfo.cs
     --Resources.resx
     --Resource.Designer.cs
  --References
  --MyLibClass.cs

--MyLibrary.Tests
  --Properties
     --AssemblyInfo.cs
  --References
  --MyLibClassTests.cs

--MainProject

测试需要设置类并将其传递给 ResourceManager,因为我正在尝试使用依赖注入。当我尝试在测试中使用下面的代码加载时,出现以下错误。

由于资源嵌入在 MyLibrary.dll 中,MyLibraryTest.dll 如何访问它?

  resmgr = new ResourceManager("MyLibrary",
                        Assembly.GetExecutingAssembly());

NUnit 中的错误

MyLibrary.Tests. MyLibClassTests.IsInValidMyProperty_Blank:
An unexpected exception type was thrown
Expected: System.ArgumentException
 but was: System.Resources.MissingManifestResourceException : Could not find any
resources appropriate for the specified culture or the neutral culture.  Make sure
"MyLibrary.resources" was correctly embedded or linked into assembly "MyLibrary.Tests"
at compile time, or that all the satellite assemblies required are loadable and
fully signed.

因此,经过考虑,dll位于单独的项目文件夹中。

有没有更好的方法来解决这个问题?

4

1 回答 1

2

在您的示例中,您已经传递程序集以从中加载资源 - 您只是出于某种原因使用不包含您正在寻找的资源的程序集。很可能您需要传递相同的程序集MyLibClass,而不是Assembly.GetExecutingAssembly()我假设为您提供测试代码的程序集。

resmgr = new ResourceManager("MyLibrary",
    typeof(MyLibClass).Assembly);
于 2013-06-01T01:04:06.047 回答