单元测试ASP.net Page_Load [英] Unit Test ASP.net Page_Load
问题描述
如何为ASP.net中的Page_Load函数创建单元测试?
How can create a unit test for the the Page_Load function in ASP.net?
我在Visual Studio单元测试框架中使用构建.我想创建一个单元测试,以检查网页的元素及其值.
I am using build in Visual Studio Unit test frame work. I want to create a unit test that check the Elements of the web page and their values.
我了解硒及其在单元测试中的能力.
I know about selenium and its abilities in unit testing.
这是要测试WebPageControl.ascx.vb的网页:
This is the web Page to test WebPageControl.ascx.vb:
Public Class WebPageControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox.Visible = False
End Sub
End Class
这是单元测试WebPageControlTest.vb:
This is the Unit test WebPageControlTest.vb:
Public Class WebPageControlTest
Public Sub PageLoadTest()
Dim target As WebPageControl_Accessor = New WebPageControl_Accessor()
Assert.IsFAlse(target.TextBox.Visible)
End Sub
End Class
执行完此操作后,我仍然会出现错误
After I do this I still get an error
Test method RechargeTest.WebPageControlTest.PageLoadTest threw exception: System.NullReferenceException: Object reference not set to an instance of an object.
推荐答案
您可能将无法在ASP.NET运行时之外新建Page.
You're probably not going to be able to new up a Page outside of the ASP.NET runtime.
您可能想在ASP.NET下四处搜索MVP(模型-视图-演示器)模式.恕我直言,它使测试Web代码变得容易得多.本文是一个很好的起点:
You may want to google around for the MVP (Model-View-Presenter) pattern under ASP.NET. It makes testing web code a lot easier IMHO. This article is a good starting point:
这篇关于单元测试ASP.net Page_Load的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!