3

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);      
    }
}
2
  • 1
    extract x.ParseXml(source) to local variable, parse 5 times is too overhead :) Commented Sep 13, 2015 at 7:01
  • FYI, your question is about unit testing, not TDD. TDD is writing tests first - in your case, you have already written your application. Commented Sep 14, 2015 at 3:31

1 Answer 1

2

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:

  1. 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.

  2. 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

Sign up to request clarification or add additional context in comments.

5 Comments

ok thank you. any advice how to unit test if a wrong file or non-existing file inputed.new to unit testing sorry
Thank you having a look now
@TomGill, Welcome, consider accepting the answer if it helped by clicking the check button. Pointing since you are a new user.
how do I test the filenot found not sure how to do it
@TomGill, just try to read from the file and that should do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.