如何测试 ASP.NET 代码隐藏
我们正在重写遗留代码,但目前我想知道如何测试代码隐藏方法。
详细信息:
- 未使用 aspx 控件。
- 事实上,代码隐藏读取 html 文件,从数据库填充数据并将最终的
html 写入响应。
我知道代码隐藏方法是可测试的,但是如何测试呢?有什么工具吗?您能提供一些参考吗?谢谢。
We are in the process of re-writing legacy code, but currently I am wondering how to test code-behind methods.
Detail:
- No aspx controls are used.
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上,代码隐藏不应包含任何繁重的逻辑。后面的代码应该了解 UI 控件的初始化、通过处理事件更新 UI 等。
我建议将功能单元提取到单独的类中,然后只测试这些类本身。在您的情况下,我至少看到两个独立的抽象功能单元:
如果您正在寻找测试 UI 行为,您可以使用特定框架,例如 等待:
编辑:评论的回答
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:
If you are looking for testing UI behavioud you can use specisl frameworks like WatiN:
EDIT: Answer to comments
DataAccessLayer
- all related tests would be great to place in the separateDataAccessLayer.Tests
project我认为您应该看看 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.