2013-06-01 24 views
2

我正在嘗試編寫一個單元測試,以訪問與項目關聯的資源。這是一般結構。資源具有旨在由MyLibClass使用字符串和類設置爲en-US如何從另一個(測試).Net dll訪問.Net DLL中的嵌入式資源?

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的是在不同的項目文件夾。

有沒有更好的方法來解決這個問題?

+0

executingassembly是測試的DLL,你想,你正在測試 –

回答

2

在您的示例中,您已經傳遞程序集來加載資源 - 您只是出於某種原因使用不包含正在查找的資源的程序集。最有可能你需要通過MyLibClass所在的同一個程序集,而不是Assembly.GetExecutingAssembly(),我假設你給出了測試代碼的彙編。

resmgr = new ResourceManager("MyLibrary", 
    typeof(MyLibClass).Assembly); 
+0

有道理的組裝,但它告訴我,「MyLibClass」是一個命名空間,但像一個「類型」中。 –

+0

@RichShealer - 這是你的代碼我不知道哪個類在那個資源所在的程序集中。通常類名與文件名匹配,所以我假定「MyLibClass」是類的名稱,但它似乎是代碼中的名稱空間。只需在該程序集中找到一個班級並使用它。請注意,如果您將類和名稱空間命名爲相同的名稱,則由於名稱解析不時會出現一些令人困惑的問題。 –

+0

這有幫助。我從來沒有想到我需要一堂課。我認爲我需要更高級的東西來識別模塊。 –