目标是在这些Xml文件中给出一些数据来运行一些测试.
如何在单元测试方法中轻松地将给定的Xml文件加载到XmlDoc中?
目前的状态是:
XmlDocument doc = new XmlDocument();
string xmlFile = "4.xml";
string dir = System.IO.Directory.GetCurrentDirectory() + @"\Msgs\"
//dir is then the value of the current exe's path, which is
//d:\sourcecode\myproject\TestResults\myComputer 2009-10-08 16_07_45\Out
//we actually need:
//d:\sourcecode\myproject\Msgs\
doc.Load( dir + fileName); //should really use System.IO.Path.Combine()!
Run Code Online (Sandbox Code Playgroud)
只是将这条路径放在一个简单的问题上app.config?考虑到开发人员机器上存在不同路径的可能性,我希望避免这种情况.
问题:如何编写算法以将单个测试方法中的给定Xml文件加载到XmlDocument中?
我正在使用具有图像上传功能的MVC网站,我想编写一个将上传图像的测试.
我创建了一个名为TestImage.jpg的图像,并将Copy to Output设置为"Copy if Newer".在我的测试中,我尝试使用以下代码加载它:
System.Drawing.Image testImage = System.Drawing.Image.FromFile(@"TestImage.jpg");
Run Code Online (Sandbox Code Playgroud)
"复制到输出"不应该将其复制到运行测试的同一目录中吗?如果没有,我怎样才能找到它被复制到的位置?最好的是某种项目根相对路径,所以我可以随意移动解决方案而不会破坏.