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












0















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.










share|improve this question



























    0















    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.










    share|improve this question

























      0












      0








      0


      0






      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.










      share|improve this question














      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.







      wpf vb.net unit-testing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 13:53









      JanJan

      6341122




      6341122
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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})





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282565%2fhow-to-unit-test-a-wpf-triggeractions-invoke-method%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            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})





            share|improve this answer






























              1














              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})





              share|improve this answer




























                1












                1








                1







                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})





                share|improve this answer















                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})






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 '18 at 14:37

























                answered Nov 13 '18 at 14:31









                mm8mm8

                83.1k81831




                83.1k81831






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282565%2fhow-to-unit-test-a-wpf-triggeractions-invoke-method%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Full-time equivalent

                    Bicuculline

                    What is this shape that looks like a rectangle with rounded ends called?