单元测试应用程序生命周期事件
我创建了一个继承System.Web.UI.Page 的类。
在该类中,我重写了一些应用程序生命周期事件,例如 page_load 和 page_init。在调用这些事件的基础之前,我会检查会话值、cookie 等。
我想对检查会话和 cookie 的方法进行单元测试。我已经能够模拟会话和 cookie 对象。是否可以对应用程序生命周期事件进行单元测试?或者是我完全重构该类的唯一选择,以便从生命周期事件中调用的所有方法都位于单独的类中?
I created a class that inherits System.Web.UI.Page.
In that class I override some application lifecycle events like page_load and page_init. Before calling the base of those events I check session values, cookies and things like that.
I would like to Unit test the methods that check the sessions and cookies. I am already able to mock the session and cookie objects. Is it possible to Unit test the application life cycle events? Or is my only option to totally re-factor the class so that all methods called from within the life cycle events are in a separate class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您能够模拟相关对象,则可以直接调用生命周期方法。但我不知道这是否是一个好主意;恕我直言,任何单个生命周期事件处理程序都应该执行多个任务,每个任务可能应该委托给其他一些类 - 并且您应该对这些代码进行单元测试。
我个人的习惯是直接测试实用程序/服务/存储库代码,并直接将代码保留在生命周期事件中,仅适用于集成测试,而不是单元测试。
If you are able to mock the pertinent objects, you could simply call the lifecycle methods directly. But I don't know if that's a good idea; IMHO, any single lifecycle event handler should be doing multiple tasks, each of which should probably be delegated to some other class - and it is that code you should be unit testing.
My personal habit is that I test utility/service/repository code directly, and leave code in the lifecycle events directly only that would apply under integration tests, rather than unit tests.