Reputation: 41
I have a asp.net app that you upload a xml filenusing the fileupload control, and it is parsed into a list of objects and displayed in a grid view pretty simple stuff. but I have been asked to write some unit tests. Im new to tdd and would like sum advice on how to write some unit tests.
This is what I have so far. Which I hard coded the xml file. Is there a way to unit test for the fileupload
[TestClass]
public class UnitTest1
{
string source = @"C:\Users\\Desktop\Web Application\ Web Application.Tests\LunchTest.xml";
[TestMethod]
public void ParseXmlTest()
{
XmlUlitilyClass x = new XmlUlitilyClass();
Assert.AreEqual(x.ParseXml(source).Count, 4);
Assert.AreEqual(x.ParseXml(source)[3].Name,"Roast of the day");
Assert.AreEqual(x.ParseXml(source)[3].DesertIncluded, true);
Assert.AreEqual(x.ParseXml(source)[0].Calories, 350);
Assert.AreEqual(x.ParseXml(source)[1].Vegetarian, false);
}
}
Upvotes: 3
Views: 3558
Reputation: 77926
Is there a way to unit test for the fileupload
Well, you definitely can't unit test the event handlers and other out bound stuff in ASP.NET
like in your case the FileUpload
control. Moreover you don't need to write unit test for fileupload control rather just check postivie/negative cases of the main logic. Yes, you can very well have the xml file name hard coded. Per your post what I see is, the main business logic here is:
unit testing the file handling logic. That is test what if a wrong file (or) non-existing file inputed. You can create separate unit test method for +ve/-ve cases where you can provide separate hardcoded file input.
XML parsing logic.
You can consider refactoring your code and put this two logic in separate method and unit test those method instead.
Per your comment, if you pro provide wrong file name then for sure runtime will throw an exception named FileNotFoundException
you can catch it in your test method by using ExpectedException
attribute. Example:
string source = @"C:\Users\\Desktop\Web Application\ Web Application.Tests\LunchTest.xml";
[TestMethod]
[ExpectedException(typeof(FileNotFoundException),
"Provided File doesn't exists.")]
public void TestFileNotExists()
{
//your code goes here
}
Read more about unit testing in MSDN A Unit Testing Walkthrough with Visual Studio Team Test
Upvotes: 2