Thursday, April 10, 2008

Unit Testing ASP.Net MVP Design Pattern using MS test Framework

For GMWBPresenter supporting GMWB User Control, MS Test framework can be used to enable access and test view components such as label, textbox, etc.

(1) We will need a Base Accessor from MS Test Framework in order to access private
and protected member on user controls, namely PrivateObject:

Friend Class BaseAccessor

Protected m_privateObject As PrivateObject

Protected Sub New(ByVal target As Object, ByVal type As PrivateType)
MyBase.New()
m_privateObject = New PrivateObject(target, type)
End Sub

Protected Sub New(ByVal type As PrivateType)
Me.New(Nothing, type)
End Sub

Friend Overridable ReadOnly Property Target() As Object
Get
Return m_privateObject.Target
End Get
End Property

Public Overrides Function ToString() As String
Return Me.Target.ToString
End Function

Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
If GetType(BaseAccessor).IsInstanceOfType(obj) Then
obj = CType(obj, BaseAccessor).Target
End If
Return Me.Target.Equals(obj)
End Function

Public Overrides Function GetHashCode() As Integer
Return Me.Target.GetHashCode
End Function

Public ReadOnly Property PrivateObject() As PrivateObject
Get
Return Me.m_privateObject
End Get
End Property
End Class

(2) For specific control access we need extend base:

Friend Class GMWBRiderControlAccessor
Inherits BaseAccessor
Protected Shared m_privateType As PrivateType = New PrivateType(GetType(GmwbRiderControl))

Friend Sub New(ByVal target As GmwbRiderControl)
MyBase.New(target, m_privateType)
End Sub

End Class

(3) To utilize MS Test framework without loading ASP.Net Host, all view elements
have to be initialized using its contructor:

_
Public Class GMWBPresenterTester
Inherits PresenterTesterBase

Private _gmwbUCA As New GMWBRiderControlAccessor(New GmwbRiderControl())

_
Public Sub OnlyIndicatorBSeeGMWBMessage()
Dim manulifePrincipal As JHAPrincipal
manulifePrincipal = AuthenticationFacade.Instance.LogonUser _
( _
"....", _
"..." _
)
Thread.CurrentPrincipal = manulifePrincipal
Dim cif As New ContractInquiryFacade
Dim rider As New ContractRiderList

rider = cif.GetContractRiders(New ContractSearch("NAS", "789065"))
_gmwbUCA.PrivateObject.SetFieldOrProperty("dsRiders", rider)
_gmwbUCA.PrivateObject.SetFieldOrProperty("lnkHelp", New Global.System.Web.UI.WebControls.HyperLink())
_gmwbUCA.PrivateObject.SetFieldOrProperty("pnlGMWBMessage", New Global.System.Web.UI.WebControls.Panel())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblGMWBMessage", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("dvGMWB", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("spnGMWB", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblGMWBValue", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("dvWithdrwalAmount", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("spnWithdrwalAmount", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblWithdrawalAmountValue", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("dvRemainingAmount", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("spnRemainingAmount", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblRemainingAmountValue", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("pnlPayments", New Global.System.Web.UI.WebControls.Panel())
_gmwbUCA.PrivateObject.SetFieldOrProperty("spnPayments", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblPaymentsValue", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("pnlYearsValue", New Global.System.Web.UI.WebControls.Panel())
_gmwbUCA.PrivateObject.SetFieldOrProperty("spnYears", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblYearsValue", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("spnLIA", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblLIA", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("spnRLIA", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblRLIA", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("spnDateLastStepUp", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblDateLastStepUp", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("spnNextStepUpDate", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblNextStepUpDate", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("spnStepUpOverride", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblStepUpOverride", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("spnPreviousGMWBRider", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblPreviousGMWBRider", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("spnRiderTradeInDate", New Global.System.Web.UI.HtmlControls.HtmlGenericControl())
_gmwbUCA.PrivateObject.SetFieldOrProperty("lblRiderTradeInDate", New Global.System.Web.UI.WebControls.Label())
_gmwbUCA.PrivateObject.SetFieldOrProperty("litDisclaimer", New Global.System.Web.UI.WebControls.Literal())
_gmwbUCA.PrivateObject.SetFieldOrProperty("litDisclaimer", New Global.System.Web.UI.WebControls.Literal())
Dim r As ContractRiderList.gmwbRow = DirectCast(rider.gmwb.Select()(0), ContractRiderList.gmwbRow)
r.vantage_failure_indicator = False
_gmwbUCA.PrivateObject.Invoke("BindControls", rider)

Dim pnlGMWBMessage As Panel = DirectCast(_gmwbUCA.PrivateObject.GetFieldOrProperty("pnlGMWBMessage"), Panel)
MSU.Assert.IsTrue(pnlGMWBMessage.Visible, "Indicator=B should be able to see GMWB Message if any")

'7889078 has GMWB ind=F
rider = cif.GetContractRiders(New ContractSearch("NAS", "676767"))
r = DirectCast(rider.gmwb.Select()(0), ContractRiderList.gmwbRow)
r.vantage_failure_indicator = False
_gmwbUCA.PrivateObject.SetFieldOrProperty("dsRiders", rider)
_gmwbUCA.PrivateObject.Invoke("BindControls", rider)

pnlGMWBMessage = DirectCast(_gmwbUCA.PrivateObject.GetFieldOrProperty("pnlGMWBMessage"), System.Web.UI.WebControls.Panel)
MSU.Assert.IsTrue(Not pnlGMWBMessage.Visible, "Indicator=F should not see GMWB Message")

End Sub

End Class

Note that App.Config under MS Test project must include all the Authorization, Capability and connection information. Also all context access such as HttpContext,
Session Context are not allowed or need to turn off for this kinds of testing strategy.

No comments: