在我的测试项目中,我有一个ViewModel,它需要从应用程序内的ressource文件中加载对象列表:
private List<XapBus> staticBusList = new List<XapBus>();
public XapEditorViewModel(ObservableCollection<XapDevice> deviceList, ObservableCollection<XapBus> busList)
{
DeviceList = deviceList;
BusList = busList;
string imagePath = "pack://application:,,,/Settings/BusSettingsSourceItems.xml";
System.Windows.Resources.StreamResourceInfo imageInfo = System.Windows.Application.GetResourceStream(new Uri(imagePath));
var stream = new StreamReader(imageInfo.Stream, false);
XmlSerializer ser = new XmlSerializer(typeof(List<XapBus>));
staticBusList = (List<XapBus>)ser.Deserialize(stream);
stream.Close();
}
在我的测试方法中,我初始化了一个ViewModel:
[TestMethod]
public void ViewModel_BusListIsNull_ReturnsFalse()
{
//Arrange
XapEditorViewModel VM = new XapEditorViewModel();
//Act
VM.BusList = null;
bool isOK = VM.AddBus(new XapBus());
//Assert
Assert.IsFalse(isOK);
}
运行测试时,出现System.UriFormatException。
我发现了一个使用这个的stackoverflow问题:
[TestInitialize]
public void OnTestInitialize()
{
UriParser.Register(new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1);
}
我添加了它,但没有成功...
有什么想法吗?
谢谢!