简体   繁体   English

如何在ASP.NET WebForms中实现TDD

[英]How to implement TDD in ASP.NET WebForms

I know that the reason that Microsoft came out with ASP.NET MVC was to make it simpler to do Test Driven Design (TDD) for ASP.NET. 我知道微软推出ASP.NET MVC的原因是为了简化为ASP.NET进行测试驱动设计(TDD)。 However, I have a rather large brown field (existing) application in ASP.NET WebForms that I would love to implement some TDD type functionality in. I'm assuming that there IS a way to do this, but what are some viable options? 但是,我在ASP.NET WebForms中有一个相当大的棕色字段(现有)应用程序,我很乐意在其中实现一些TDD类型的功能。我假设有一种方法可以做到这一点,但有哪些可行的选择?

Microsoft introduced ASP.NET MVC because they thought they could make money from an untapped market - those who feel that Web Forms are too "heavyweight", and who are programming using a lighter-weight framework. 微软推出了ASP.NET MVC,因为他们认为他们可以从尚未开发的市场赚钱 - 那些认为Web Forms过于“重量级”的人,以及使用轻量级框架进行编程的人。 This includes those who are accustomed to the MVC paradigm. 这包括习惯于MVC范例的人。

It also includes those who couldn't figure out how to do unit tests in web forms, and who want to use unit tests and TDD. 它还包括那些无法弄清楚如何在Web表单中进行单元测试,以及谁想要使用单元测试和TDD的人。

The way to do it with web forms, as with anything else, is to separate everything but the UI code into separate classes in a class library. 与其他任何东西一样,使用Web表单的方法是将除UI代码之外的所有内容分成类库中的单独类。 Use TDD to develop those classes. 使用TDD开发这些类。

The next layer of controversy is whether it's necessary to use TDD to develop the remainder of the code: the markup, client-side code, user interactions, etc. My answer is that if you've got the rest isolated and tested, that it's not worth the trouble to use TDD for this. 下一层争议是是否有必要使用TDD来开发代码的其余部分:标记,客户端代码,用户交互等。我的答案是,如果你已经隔离并测试了其余部分,那就是不值得为此使用TDD的麻烦。

Consider: your pages need to have a particular appearance. 考虑一下:您的页面需要具有特定的外观。 Are you going to write a failing unit test to prove that you are using CSS correctly? 您是否要编写一个失败的单元测试来证明您正确使用CSS? To prove that you're using the correct CSS styles? 要证明您使用的是正确的CSS样式? I don't think so. 我不这么认为。


To clarify: In TDD, we start with a failing unit test. 澄清:在TDD中,我们从失败的单元测试开始。 We then make the simplest possible changes that will make the test succeed. 然后,我们进行最简单的更改,使测试成功。

Imagine using TDD for a web page. 想象一下,将TDD用于网页。 What failing tests will you produce? 你会做什么样的失败测试?

  1. Test that page is well-formed HTML 测试该页面是格式良好的HTML
  2. Test that page includes the correct title 测试该页面包含正确的标题
  3. Test that page includes 测试该页面包括
    1. "Enter ID" label “输入ID”标签
    2. An id textbox 一个id文本框
    3. A data grid 数据网格
    4. A "Go" button 一个“开始”按钮
  4. Test that data grid is empty after a GET 在GET之后测试数据网格是否为空
  5. Test that grid loads with data from customer 1 when "1" is entered into the text box and "Go" is clicked. 当“1”输入文本框并单击“Go”时,测试网格加载来自客户1的数据。

And none of the above tests for the appearance of the page. 并且没有上述测试页面的外观。 None of it tests the client-side behavior of any JavaScript on the page. 它们都没有测试页面上任何JavaScript的客户端行为。

I think that's silly. 我觉得这很傻。 Instead, test your DAL method that retrieves data based on the ID. 而是测试您的DAL方法,该方法根据ID检索数据。 Make sure it returns the correct ID for id 1. Then, how long will it take to manually test the page to make sure it looks correct, you can enter the "1" and click "Go", and that the data that appears in the grid is the correct data for customer 1? 确保它返回id 1的正确ID。然后,手动测试页面以确保它看起来正确需要多长时间,您可以输入“1”并单击“Go”,以及显示的数据网格是客户1的正确数据?

Test-Driven Development and automated unit tests are meant to test behavior. 测试驱动开发和自动化单元测试旨在测试行为。 The UI of a web form is mostly declarative. Web表单的UI主要是声明性的。 There's a large "impedance mismatch" here. 这里有一个很大的“阻抗不匹配”。

First of all, its really hard to test WebForms. 首先,它很难测试WebForms。 But if you break out logic to Controllers/Presenters like the MVC/MVP pattern you can at least test the presenters. 但是,如果您将控制器/演示者的逻辑分解为MVC / MVP模式,则至少可以测试演示者。

Pseudo-code 伪代码

Default.aspx Default.aspx的

public void Page_Init(object sender, EventArgs e) {
 _presenter = new DefaultPresenter(IDependencyOne, IDependencyTwo etc) //Init new presenter, either from IoC container of choose or "new-it-up"
}

public void Save() {
 _presenter.Save(txtValue.Text)
}

Than you can easily test the presenter in isolation at least =) 比你可以轻松地在隔离测试演示者至少=)

You can run HTTP-based tests to test the high level functionality. 您可以运行基于HTTP的测试来测试高级功能。 The best thing would be to encapsulate code-behind code into your business layer and run unit tests on it. 最好的办法是将代码隐藏代码封装到业务层并对其运行单元测试。

您可以使用官方的Unittest Framework: http//msdn.microsoft.com/en-us/library/ms182526.aspx

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

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