2013-05-28 112 views
2

試圖遵循這一點:http://msdn.microsoft.com/en-us/library/21a15yht.aspx該示例的問題在於它是visual studio用戶和命令行用戶的指令混合,但我盡我所能只遵循VS指令。簡單的ResourceManager示例不能在單元測試中工作

所以我有:在視覺工作室創建一個新的單元測試項目,名爲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)>.

相同的代碼在控制檯應用程序中工作得很好。

+0

您的資源文件名是Greetings(複數),但您以單數形式請求資源 - 問候語。雖然我很驚訝你的代碼在控制檯中工作。可能是你在這個問題上犯了一個錯字? – vlad2135

+0

@ vlad2135是的錯字,謝謝。 – weston

回答

1

嗯,我認爲這是因爲你的單元測試工具shadow複製主程序集,但不復制衛星。 Enable shadow copying for satellite assembly並重新運行單元測試。

確認我的猜測檢查Assembly.GetExecutingAssembly()。的值.Codebase的位置。

+0

啊,我沒有發現有影子組件正在創建。 – weston

+0

如果我的答案解決了您的問題,請接受答案。 – vlad2135