Unit Test ASP.net Page_Load

How to create unit test for Page_Load function in ASP.net?

I use the built-in Visual Studio unit test framework. I want to create a unit test that checks Webpage Elements and their values.

I know about selenium and its ability in unit testing.

This is the webpage for checking 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 

This is 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 that, I still get the error message

 Test method RechargeTest.WebPageControlTest.PageLoadTest threw exception: System.NullReferenceException: Object reference not set to an instance of an object. 
+6
source share
3 answers

You probably won't be able to refresh the page outside of the ASP.NET runtime.

You might want to google for the MVP (Model-View-Presenter) template in ASP.NET. This makes testing web code much easier IMHO. This article is a good starting point:

http://haacked.com/archive/2006/08/09/ASP.NETSupervisingControllerModelViewPresenterFromSchematicToUnitTestsToCode.aspx

+2
source

If you need ASP.NET WebForms test code, check out this project. http://webformsmvp.com/

+2
source

Source: https://habr.com/ru/post/885667/


All Articles