简体   繁体   English

如何单元测试ASP.NET Web窗体

[英]How to unit test ASP.NET Web Forms

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. 我有一个asp.net应用程序,你使用fileupload控件上传一个xml文件,它被解析成一个对象列表,并在网格视图中显示非常简单的东西。 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. 我是tdd的新手,想了解如何编写一些单元测试的建议。

This is what I have so far. 这就是我到目前为止所拥有的。 Which I hard coded the xml file. 我对xml文件进行了硬编码。 Is there a way to unit test for the fileupload 有没有办法对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);      
    }
}

Is there a way to unit test for the fileupload 有没有办法对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. 好吧,你绝对不能单独测试ASP.NET的事件处理程序和其他绑定内容,就像在你的情况下FileUpload控件一样。 Moreover you don't need to write unit test for fileupload control rather just check postivie/negative cases of the main logic. 此外,您不需要为fileupload控件编写单元测试,而只需检查主逻辑的postivie / negative案例。 Yes, you can very well have the xml file name hard coded. 是的,你可以很好地将xml文件名硬编码。 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. 您可以为+ ve / -ve情况创建单独的单元测试方法,您可以在其中提供单独的硬编码文件输入。

  2. XML parsing logic. XML解析逻辑。

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. 根据您的评论,如果您提供错误的文件名,那么运行时肯定会抛出一个名为FileNotFoundException您可以使用ExpectedException属性在测试方法中捕获它。 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 阅读有关MSDN中单元测试的更多信息使用Visual Studio Team Test进行单元测试演练

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM