lundi 27 juin 2016

Inherit from System.Timers.ElapsedEventArgs to make unit testing easier

When I try to inherit from System.Timers.ElapsedEventArgs I'm not able to make a valid class because the only constructor is internal (gotten from 1)

I want to do this because I want to be able to create an instance of the class I'm deriving, and pass that as an argument when Invoking the Timer's elapsed event for unit testing.

To help with the testing I created a layer of indirection with a different event as a property on the class which I can invoke using a public method. I know this isn't the right way to do things right now but I want to get the test working and them I'm going to figure out a way to substitute the system clock/timer.

The new event as a property:

public event System.Timers.ElapsedEventHandler PerformEvent;

The chained events. This way it fires if time elapses or I can invoke it on my own:

this.m_timer.Elapsed += this.PerformEvent;

I'm trying to create a public way of invoking it and can't new up a ElapsedEventArgs.

    public void FireEvent()
    {
        object o = new object();
        this.PerformEvent.Invoke(o, objectIWantToBeElapsedEventArgs)
    }

1: http://ift.tt/28YZmxR

   internal ElapsedEventArgs(int low, int high) {        
        long fileTime = (long)((((ulong)high) << 32) | (((ulong)low) & 0xffffffff));
        this.signalTime = DateTime.FromFileTime(fileTime);                        
    }

Aucun commentaire:

Enregistrer un commentaire