如何使 ASP.NET Webforms 应用程序可测试?
我正在查看一个使用 ASP.NET 编写的遗留企业应用程序。没有控件或 Web 表单。它是这样工作的:
EmployeeList.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmployeeList.aspx.cs" Inherits="EmployeeList" %>
EmployeeList.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
// Security Check
// Load Template, get version 1.4 of active employee list
StringBuilder template = GetTemplate("Employee", "ActiveList", "1.4", true);
// Get list from database
using(SqlDataReader objReader = GetListFromDB())
{
while(objReader.Read())
{
//fills data
TUtils.Replace(template, ROW, "%name%", objReader[0]);
}
}
// return response
Response.Write(template.ToString());
}
private StringBuilder GetTemplate(string x, string y, string v, bool z);
{
// returns template
}
private SqlDataReader GetListFromDB() {
// returns data reader
}
我的问题是,由于我们没有使用 Web 表单,有没有办法在中引入 NUnit这个事件驱动模型(如上图)?
另外,请避免建议转向 ASP.NET MVC 或其他模式,我们正在考虑这些模式,但想知道是否有任何方法可以将此企业应用程序转换为可测试的。
I am looking at a legacy enterprise application, which written using ASP.NET. No controls or web forms. This is how it works:
EmployeeList.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmployeeList.aspx.cs" Inherits="EmployeeList" %>
EmployeeList.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
// Security Check
// Load Template, get version 1.4 of active employee list
StringBuilder template = GetTemplate("Employee", "ActiveList", "1.4", true);
// Get list from database
using(SqlDataReader objReader = GetListFromDB())
{
while(objReader.Read())
{
//fills data
TUtils.Replace(template, ROW, "%name%", objReader[0]);
}
}
// return response
Response.Write(template.ToString());
}
private StringBuilder GetTemplate(string x, string y, string v, bool z);
{
// returns template
}
private SqlDataReader GetListFromDB() {
// returns data reader
}
My question is, since we are not using web forms, is there a way to introduce NUnit in this event driven model (as shown above)?
Also, please avoid suggestions to move to ASP.NET MVC or other patterns, which we are considering, but wondering is there any way to convert this enterprise application testable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是绝对有可能的。您应该了解如何使用 ASP.NET Webforms 实现 MVP 模式。有几种开源实现,但您可以自己做一个较小的专门实现。
基础知识是将逻辑背后的代码移至演示者类。演示者类具有对实现接口的页面的引用。您的情况的技巧是模拟 Page.Response 对象以进行测试。这就是为什么很难以正确的方式对其进行单元测试。 PageResponse 属性包含一个从 HttpResponseBase 派生的对象,您应该在测试中模拟该基类,并在示例中进行断言。您可以从这里开始,然后使用会话、请求等功能扩展您的演示者。
如果您根本没有任何标记,可能您可以只在视图构造函数中创建演示者,而不必费心拥有和引用看法。
澄清一下:最大的技巧是从 aspx.cs 文件中获取代码。那头野兽是不可测试的。
Presenter 的示例基类:
This is absolutely possible. You should have a look on implementing MVP pattern with ASP.NET Webforms. There are several open source implementations but you can do a smaller specialized on your your own.
The basics are to move your code behind logic to a presenterclass. The presenter class has a reference to the page implementing an interface. The trick in your case will be to Mock the Page.Response object for your test. Thats why it´s hard to unit test it right way. The PageResponse Property contains a object deriving from HttpResponseBase and that´s the baseclass you should Mock in your tests and do your asserts on with your example. You could start with that and then extend your presenter with functionalty like Session, Request etc.
If you don´t have any markup at all probably you can just create the presenter in the view constructor and don´t bother of having and reference to the view.
To clarify: The big trick is to get the code out of the aspx.cs file. That beast is not testable.
Sample base class for Presenters:
由于您的大部分代码都位于代码隐藏中,因此我认为使用 NUnit / Visual Studio 测试框架的常用测试方法不会很好地工作。
然而,我认为一种可能的方法是使用 UI 测试框架,例如 WATIN / 硒。这将使您仍然能够为各种功能编写测试。
我过去做过类似的事情,基本上为每个导致服务器回发的 UI 操作编写一个测试用例。可能并不理想,但它确实允许您自动化测试。
Since most of your code is in the code-behind, I dont think that the usual testing approach with NUnit / Visual Studio testing framework will work well.
However, I think one possible approach is to use UI Testing frameworks like WATIN / Selenium. This will enable you to still write tests for the various functionality.
I have done something similar in the past by basically writing a test case for every UI action that results in a server postback. May not be ideal but it does allow you to automate your testing.