我正在尝试编写一个单元测试来访问与项目关联的资源。这是一般结构。该资源具有供 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,因为我正在尝试使用依赖注入(inject)。当我尝试在测试中使用下面的代码加载时,出现以下错误。
由于资源嵌入在 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 位于单独的项目文件夹中。
有更好的方法来解决这个问题吗?
最佳答案
在您的示例中,您已经传递程序集来从中加载资源 - 您只是出于某种原因使用不包含您正在查找的资源的程序集。您很可能需要传递 MyLibClass
所在的相同程序集,而不是 Assembly.GetExecutingAssembly()
,我认为它为您提供了测试代码的程序集。
resmgr = new ResourceManager("MyLibrary",
typeof(MyLibClass).Assembly);
关于c# - 如何从另一个(测试).Net DLL 访问 .Net DLL 中的嵌入资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16867882/