mardi 5 juin 2018

c# Unit testing Event and Delegate

This is an academy work where at this point I would like to test that an event is raised and maybe count the number of times it was raised.

I have a class as observer that will be responsive to raise the events and in the class that listens to said events, I have it subscribing to the event and i have a callback function when the event is raised.

I have the current code for an Observer class:

public class AbstractDataMapperObserver {
        public enum Access { LAZY, EAGER };
        public event EventHandler<NewAccessEventArgs> EagerDataMapperAcces=
s;
        public event EventHandler<NewAccessEventArgs> LazyDataMapperAccess=
;

        private void onDataMapperAccess( NewAccessEventArgs e) {
            if (e.PAccess == Access.EAGER) {
            EventHandler<NewAccessEventArgs> temp = Volatile.Read(ref Ea=
gerDataMapperAccess);
                if(temp != null) {
                    temp(this, e);
                }

            } else if (e.PAccess == Access.LAZY) {
                EventHandler<NewAccessEventArgs> temp = Volatile.Read(re=
f LazyDataMapperAccess);
                if (temp != null) {
                    temp(this, e);
                }
            }
        }

        public class NewAccessEventArgs : EventArgs{
            private readonly Access access;

            public NewAccessEventArgs(Access nAccess) {
                access = nAccess;
            }

            public Access PAccess { get { return access; } }
        }

        public void SimulateDataAccess(Access sAccess) {
            NewAccessEventArgs e = new NewAccessEventArgs(sAccess);
            onDataMapperAccess(e);
        }
    }

and the class that does something when an event is raised:

public abstract class AbstractDataMapper<K, V> : IDataMapper<K, V> {
        protected readonly string connStr;
        protected readonly DataSet cache;
        AbstractDataMapperObserver observer;

        public AbstractDataMapper(string connStr) : this(connStr, true) {
            this.connStr = connStr;
        }

        public AbstractDataMapper(string connStr, bool withCache) {
            observer = new AbstractDataMapperObserver();
            observer.LazyDataMapperAccess += LazyDataAccess;
            this.connStr = connStr;
            if (withCache)
                cache = new DataSet();
        }

        protected abstract string SqlGetById(K id);

        ...

        public V GetById(K id) {
            observer.SimulateDataAccess(Access.LAZY);
            string sql = SqlGetById(id);
            IEnumerator<V> iter = Get(sql).GetEnumerator();
            V result = iter.MoveNext() ? iter.Current : default(V);
            return result;
        }

        ...

        //TODO Change what method does
        private void LazyDataAccess(Object sender, NewAccessEventArgs e)
        {
            Debug.WriteLine("Lazy database access");
            Debug.Write("Access:" + e.PAccess);
        }
    }

I am having quite the doubts about what the last function could do so that i am able to test that the event is being raised

Aucun commentaire:

Enregistrer un commentaire