// This helper will override all the open methods of the MockCommunicationObject
    // and record their names into the provided list in the order they are called.
    public static void InterceptAllCloseMethods(IMockCommunicationObject mco, List <string> methodsCalled)
    {
        mco.OnClosingOverride = () =>
        {
            methodsCalled.Add("OnClosing");
            mco.DefaultOnClosing();
        };

        mco.OnCloseOverride = (TimeSpan t) =>
        {
            methodsCalled.Add("OnClose");
            mco.DefaultOnClose(t);
        };

        mco.OnBeginCloseOverride = (TimeSpan t, AsyncCallback c, object s) =>
        {
            methodsCalled.Add("OnBeginClose");
            return(mco.DefaultOnBeginClose(t, c, s));
        };

        mco.OnClosedOverride = () =>
        {
            methodsCalled.Add("OnClosed");
            mco.DefaultOnClosed();
        };

        // The OnAbort is considered one of the methods associated with close.
        mco.OnAbortOverride = () =>
        {
            methodsCalled.Add("OnAbort");
            mco.DefaultOnAbort();
        };
    }
    // Intercepts all the open and close methods in MockCommunicationObject
    // and records the CommunicationState before and after the default code executes,
    public static void InterceptAllStateChanges(IMockCommunicationObject mco, CommunicationStateData data)
    {
        // Immediately capture the current state after initial creation
        data.StateAfterCreate = mco.State;

        mco.OnOpeningOverride = () =>
        {
            data.StateEnterOnOpening = mco.State;
            mco.DefaultOnOpening();
            data.StateLeaveOnOpening = mco.State;
        };

        mco.OnOpenOverride = (TimeSpan t) =>
        {
            data.StateEnterOnOpen = mco.State;
            mco.DefaultOnOpen(t);
            data.StateLeaveOnOpen = mco.State;
        };

        mco.OnBeginOpenOverride = (TimeSpan t, AsyncCallback c, object s) =>
        {
            data.StateEnterOnBeginOpen = mco.State;
            IAsyncResult result = mco.DefaultOnBeginOpen(t, c, s);
            data.StateLeaveOnBeginOpen = mco.State;
            return(result);
        };

        mco.OnOpenedOverride = () =>
        {
            data.StateEnterOnOpened = mco.State;
            mco.DefaultOnOpened();
            data.StateLeaveOnOpened = mco.State;
        };

        mco.OnClosingOverride = () =>
        {
            data.StateEnterOnClosing = mco.State;
            mco.DefaultOnClosing();
            data.StateLeaveOnClosing = mco.State;
        };

        mco.OnCloseOverride = (TimeSpan t) =>
        {
            data.StateEnterOnClose = mco.State;
            mco.DefaultOnClose(t);
            data.StateLeaveOnClose = mco.State;
        };

        mco.OnBeginCloseOverride = (TimeSpan t, AsyncCallback c, object s) =>
        {
            data.StateEnterOnBeginClose = mco.State;
            IAsyncResult result = mco.DefaultOnBeginClose(t, c, s);
            data.StateLeaveOnBeginClose = mco.State;
            return(result);
        };

        mco.OnClosedOverride = () =>
        {
            data.StateEnterOnClosed = mco.State;
            mco.DefaultOnClosed();
            data.StateLeaveOnClosed = mco.State;
        };

        #endregion helpers
    }
    // Intercepts all the open and close methods in MockCommunicationObject
    // and records the CommunicationState before and after the default code executes,
    public static void InterceptAllStateChanges(IMockCommunicationObject mco, CommunicationStateData data)
    {
        // Immediately capture the current state after initial creation
        data.StateAfterCreate = mco.State;

        mco.OnOpeningOverride = () =>
        {
            data.StateEnterOnOpening = mco.State;
            mco.DefaultOnOpening();
            data.StateLeaveOnOpening = mco.State;
        };

        mco.OnOpenOverride = (TimeSpan t) =>
        {
            data.StateEnterOnOpen = mco.State;
            mco.DefaultOnOpen(t);
            data.StateLeaveOnOpen = mco.State;
        };

        mco.OnBeginOpenOverride = (TimeSpan t, AsyncCallback c, object s) =>
        {
            data.StateEnterOnBeginOpen = mco.State;
            IAsyncResult result = mco.DefaultOnBeginOpen(t, c, s);
            data.StateLeaveOnBeginOpen = mco.State;
            return result;
        };

        mco.OnOpenedOverride = () =>
        {
            data.StateEnterOnOpened = mco.State;
            mco.DefaultOnOpened();
            data.StateLeaveOnOpened = mco.State;
        };

        mco.OnClosingOverride = () =>
        {
            data.StateEnterOnClosing = mco.State;
            mco.DefaultOnClosing();
            data.StateLeaveOnClosing = mco.State;
        };

        mco.OnCloseOverride = (TimeSpan t) =>
        {
            data.StateEnterOnClose = mco.State;
            mco.DefaultOnClose(t);
            data.StateLeaveOnClose = mco.State;
        };

        mco.OnBeginCloseOverride = (TimeSpan t, AsyncCallback c, object s) =>
        {
            data.StateEnterOnBeginClose = mco.State;
            IAsyncResult result = mco.DefaultOnBeginClose(t, c, s);
            data.StateLeaveOnBeginClose = mco.State;
            return result;
        };

        mco.OnClosedOverride = () =>
        {
            data.StateEnterOnClosed = mco.State;
            mco.DefaultOnClosed();
            data.StateLeaveOnClosed = mco.State;
        };

        #endregion helpers
    }
    // This helper will override all the open methods of the MockCommunicationObject
    // and record their names into the provided list in the order they are called.
    public static void InterceptAllCloseMethods(IMockCommunicationObject mco, List<string> methodsCalled)
    {
        mco.OnClosingOverride = () =>
        {
            methodsCalled.Add("OnClosing");
            mco.DefaultOnClosing();
        };

        mco.OnCloseOverride = (TimeSpan t) =>
        {
            methodsCalled.Add("OnClose");
            mco.DefaultOnClose(t);
        };

        mco.OnBeginCloseOverride = (TimeSpan t, AsyncCallback c, object s) =>
        {
            methodsCalled.Add("OnBeginClose");
            return mco.DefaultOnBeginClose(t, c, s);
        };

        mco.OnClosedOverride = () =>
        {
            methodsCalled.Add("OnClosed");
            mco.DefaultOnClosed();
        };

        // The OnAbort is considered one of the methods associated with close.
        mco.OnAbortOverride = () =>
        {
            methodsCalled.Add("OnAbort");
            mco.DefaultOnAbort();
        };
    }