Jan
Jan

Reputation: 4206

How to unit test a WPF Triggeraction's invoke-Method

I can't get my head around how to unit test this class' Invoke-Method:

Option Strict On
Imports System.Windows.Interactivity

Public Class ViewErrorCounterAction
    Inherits TriggerAction(Of DependencyObject)

    Public Shared ReadOnly ViewErrorCounterProperty As DependencyProperty = DependencyProperty.Register("ViewErrorCounter",GetType(Integer),GetType(ViewErrorCounterAction))

    Sub New()
        ViewErrorCounter = 0 
    End Sub

    Public Property ViewErrorCounter As Integer
        Get
            Return CType(GetValue(ViewErrorCounterProperty), Integer)
        End Get
        Set
            SetValue(ViewErrorCounterProperty, Value)
        End Set
    End Property

    Protected Overrides Sub Invoke(parameter As Object)
        Dim e = CType(parameter, ValidationErrorEventArgs)
        If (e.Action = ValidationErrorEventAction.Added) Then
            ViewErrorCounter = ViewErrorCounter + 1
        ElseIf (e.Action = ValidationErrorEventAction.Removed) Then
            ViewErrorCounter = ViewErrorCounter - 1
        End If
    End Sub

End Class

I managed to call the invoke method and also pass the right parameter by using this code but struggle with the creation of the ValidationErrorEventArgs object as it's constructor is internal:

Sub Invoke_ValidationerrorEventEinAufruf_ErrorCounterIst1()
    Dim _viewErrorCounterAction = New ViewErrorCounterAction
    dim _manualTrigger = New ManualTrigger
    _manualTrigger.Actions.Add(_viewErrorCounterAction)

    'This line won't work because the constructor is internal
    Dim ev = New ValidationErrorEventArgs(New ValidationError(Nothing, Nothing), ValidationErrorEventAction.Added)

    _manualTrigger.Invoke(ev)
    Dim result = _viewErrorCounterAction.ViewErrorCounter
    Assert.That(result, [Is].EqualTo(1))
End Sub

'with help of https://stackoverflow.com/a/12977944/2582968
Public Class ManualTrigger
    Inherits Interactivity.TriggerBase(Of DependencyObject)

    Public Sub Invoke(parameter As Object)
        Me.InvokeActions(parameter)
    End Sub
End Class

I experimented with Activator.CreateInstance to create ValidationErrorEventArgs but wasn't successful yet. Since this whole activator story seems to me as being far out from an ordinary unit test's complexity, I'd appreciate any hints on how to best test this code. If the Activator is my only option, I'd be happy about some help on how to create that ValidationErrorEventArgs-Object.

Upvotes: 0

Views: 120

Answers (1)

mm8
mm8

Reputation: 169200

You could create a ValidationErrorEventArgs using reflection:

Dim validationErrorEventArgsConstructorInfo As ConstructorInfo = GetType(ValidationErrorEventArgs) _
    .GetConstructors(BindingFlags.NonPublic Or BindingFlags.Instance) _
    .First()

Dim validationErrorEventArgsInstance As ValidationErrorEventArgs = validationErrorEventArgsConstructorInfo _
    .Invoke(New Object() {New ValidationError(New DataErrorValidationRule(), New Object()), ValidationErrorEventAction.Added})

Upvotes: 1

Related Questions