1

I am just learning about Unit Testing. I am using NUnit to build tests for a VB.NET project.

The project I'm working on is part of a framework which will be used by people who build ASP.NET websites. It includes a base class (which inherits System.Web.HttpApplication) that users of my framework will inherit their application class from.

The project also contains a number of composite controls.

I can't quite work out at the moment how you would go about writing tests for either the application base class or any of the composite controls.

In the case of the application base class, should the Unit Test project include a class which inherits from it and then test against that?

Any pointers would be appreciated!

Thanks.

4

4 回答 4

1

正如您所说,我将通过创建一个子类并对其进行测试来间接测试应用程序基类。

对于控件,我会使用 Selenium:http ://selenium.seleniumhq.org/ 。

于 2008-12-07T20:17:02.883 回答
0

It's no longer maintained but there is NUnitAsp.

[Test] 
public void TestExample() 
{ 
   // First, instantiate "Tester" objects: 
   LabelTester label = new LabelTester("textLabel"); 
   LinkButtonTester link = new LinkButtonTester("linkButton"); 

   // Second, visit the page being tested: 
   Browser.GetPage("http://localhost/example/example.aspx"); 

   // Third, use tester objects to test the page: 
   Assert.AreEqual("Not clicked.", label.Text); 
   link.Click(); 
   Assert.AreEqual("Clicked once.", label.Text); 
   link.Click(); 
   Assert.AreEqual("Clicked twice.", label.Text); 
}
于 2008-12-07T01:52:58.980 回答
0

这只是部分答案。就是你在 Django 中做这种事情的方式。我想在 ASP.NET 中有类似的东西。为了测试客户端代码,有jsUnit 之类的东西。

于 2008-12-07T03:56:38.153 回答
0

我可以通过为我正在单元测试的 Web 控件创建一个存根并调用受保护的 RenderContents() 方法并验证 HTML 来做到这一点:

[Test]
public void ConditionQueryBuilderTest_RendersProperHtml()
{
    var sw = new StringWriter();
    var queryBuilder = new ConditionQueryBuilderStub
    {
        ID = "UnitTestbuilder",
        QueryBuilderURL = @"\SomeAspxPage\SomeWebMethod",
        ResetQueryBuilderURL = @"\SomeAspxPage\OnQueryBuilderReset",
        FilterValuesCollection = new Dictionary<int, string> { {15, "Some Condition"}}
    };
    queryBuilder.RenderAllContents(new HtmlTextWriter(sw)); // This is a method in my stub that exposes RenderContents()

    AppendLog(sw.ToString());

    Assert.AreEqual(ExpectedHtml, sw.ToString());
}
于 2018-03-08T15:21:48.213 回答