我正在與另一位開發人員開發一個開源項目。我正在研究需要讀取默認自定義xml配置文件的單元測試。我只能保證回購中的路徑部分將在他的機器上。我怎樣才能重寫這個測試,使其可移植?我不希望測試每次檢查代碼時都會出現失敗。單元測試需要讀取文件。我如何使它便攜?
public class ConfigurationTests
{
[TestMethod]
public void ConfigurationLoads()
{
//todo: how do I make this test portable?
const string configFile = @"C:\Users\Christopher\Source\Repos\RetailCoderVBE\RetailCoder.VBE\Config\rubberduck.config";
var deserializer = new XmlSerializer(typeof(Rubberduck.Config.Configuration));
using (StreamReader reader = new StreamReader(configFile))
{
Rubberduck.Config.Configuration config = (Rubberduck.Config.Configuration)deserializer.Deserialize(reader);
Assert.IsNotNull(config);
}
}
}
這是我保證其他開發者將擁有的路徑的一部分。在此之前,我需要以某種方式動態獲取。
\RetailCoderVBE\RetailCoder.VBE\Config\rubberduck.config
據我所知,單元測試不應該依賴於系統外部的任何東西 - 正是因爲配置不能保證是可移植的。 – Gigi 2014-11-21 13:04:25
這個'TestMethod'應該測試什麼?現在,它只測試'XmlSerializer.Deserialize'返回'Rubberduck.Config.Configuration'的實例......但你不是'XmlSerializer'的作者,所以測試它是沒有意義的。 – 2014-11-21 13:04:37
對。這就是我問的原因。你如何建議我重寫我的測試,以便我不依賴文件?我是單元測試新手,所以我不知道在這裏做什麼@Gigi。 – RubberDuck 2014-11-21 13:07:47