如何测试 ASP.NET 代码隐藏

发布于 2024-12-08 20:26:14 字数 206 浏览 5 评论 0原文

我们正在重写遗留代码,但目前我想知道如何测试代码隐藏方法。

详细信息:

  1. 未使用 aspx 控件。
  2. 事实上,代码隐藏读取 html 文件,从数据库填充数据并将最终的html 写入响应。

我知道代码隐藏方法是可测试的,但是如何测试呢?有什么工具吗?您能提供一些参考吗?谢谢。

We are in the process of re-writing legacy code, but currently I am wondering how to test code-behind methods.

Detail:

  1. No aspx controls are used.
  2. In fact code-behind reads html file, fills data from the database and writes the final html to the response.

I understand code-behind methods are testable, but how? Any tools? Can you provide some reference please? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2024-12-15 20:26:14

基本上,代码隐藏不应包含任何繁重的逻辑。后面的代码应该了解 UI 控件的初始化、通过处理事件更新 UI 等。

我建议将功能单元提取到单独的类中,然后只测试这些类本身。在您的情况下,我至少看到两个独立的抽象功能单元:

  1. 数据访问层
  2. HTML 编写器

如果您正在寻找测试 UI 行为,您可以使用特定框架,例如 等待

受 Watir 的启发,WatiN 于 2005 年 12 月开始开发
使 .Net 可以进行类似的 Web 应用程序测试
语言。从那时起,WatiN 已发展成为一种易于使用的功能
丰富稳定的框架。 WatiN 采用 C# 开发,旨在带来
您可以通过 Internet Explorer 轻松实现测试自动化,
使用 .Net 的 FireFox

编辑:评论的回答

  • 通常,数据访问帮助程序和服务位于单独的程序集中,并由接口抽象,因此可以轻松替换和测试
  • 我喜欢每个项目都有自己单独的测试的方法项目,基本上如果您坚持使用单独的项目 DataAccessLayer - 所有相关测试都非常适合放在单独的 DataAccessLayer.Tests 项目中

Bacisally code-behind should not contains any heavy logic. Code behind should be aware of UI controls initialization, updating UI by handling events and so on.

I would suggest extracting functional units into separate classes and then just test these classes itself. In you case I see at least two standalone and abstracted functional units:

  1. Data Access Layer
  2. HTML Writer

If you are looking for testing UI behavioud you can use specisl frameworks like WatiN:

Inspired by Watir development of WatiN started in December 2005 to
make a similar kind of Web Application Testing possible for the .Net
languages. Since then WatiN has grown into an easy to use, feature
rich and stable framework. WatiN is developed in C# and aims to bring
you an easy way to automate your tests with Internet Explorer and
FireFox using .Net

EDIT: Answer to comments

  • Generally data access helpers and services are located in a separate assembly and abstracted by interfaces so easily could be substituted and tested
  • I like the approach when each project has own separate tests project, basically if you will stick with separate project DataAccessLayer - all related tests would be great to place in the separate DataAccessLayer.Tests project
仙女山的月亮 2024-12-15 20:26:14

我认为您应该看看 ASP.NET Web 窗体的 MVP(模型-视图-演示者)模式。我也在重写一个没有使用 MVP 的网站。

以下是一些链接:

希望这对您有所帮助。另外,如果您有机会阅读 Dino Esposito 的 Programming Microsoft ASP.NET 4 你会发现一个很好的章节详细解释了 MVP。

I think you should have a look at MVP (Model-View-Presenter) pattern for ASP.NET Web Forms. I am also in the process of rewriting a web site which did not use MVP.

Here are a few links:

I hope this will help you. Also, if you get a chance to read Dino Esposito's Programming Microsoft ASP.NET 4 you will find a nice chapter that explains MVP in details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文