VS2012 R#7 How to catch Exception thrown by callback using Enqueue

Oct 3, 2012 at 2:25 PM

Hi,

I'm running my unit tests from VS2012 with R#7 and I have a problem with the following test:

        [TestMethod]
        [Asynchronous]
        public void Some_Test()
        {
            var fileTestData = new FileTestData();

            Action callback = () =>
            {
                fileTestData.Done = true;
            };

            EnqueueCallback(() =>
                {
                    SomeAsyncMethodWithCallback(callback);
                });

            EnqueueConditional(() => fileTestData.Done);

            EnqueueTestComplete();
        }

SomeAsyncMethodWithCallback is a method that performs some async operation, then call the callback when done with a code like this one:

        private static void SomeMethodCompleted(object sender, SomeMethodCompletedEventArgs e)
        {
            var manager = (Manager)sender;
            manager.SomeMethodCompleted -= SomeMethodCompleted;
            var callback = (Action)manager.ContextState;
            if (e.Error != null)
            {
                throw e.Error;
            }

            {
              // do some stuff
            }
            
            if (callback != null)
                callback();
        }

When the exception is thrown in the callback (red line)

1/ I can't catch it

2/ the unit test never ends

I have the same problem if I try to send the exception on the UI thread using:

 

new DispatcherSynchronizationContext().Post(_ => { throw e.Error; }, null);

 

 

I tried to register to the AppDomain.CurrentDomain.UnhandledException in the SilverlightTestBase.Initialize() method (marked with [TestInitialize]) but it doesn't work (MethodAccessException because of Security Critical section)

 

 

Coordinator
Oct 3, 2012 at 3:11 PM

Try this version of AgUnit, It has better error handling:
https://github.com/downloads/sdekock/AgUnit/AgUnit-0.6.1-for-ReSharper-7.0.zip

UnhandledException in Silverlight you need to do on Application:
Application.Current.UnhandledException += ...

I assume this problem originates in StatLight, which is what AgUnit uses under the covers.
You might have better luck searching through their issues / discussions.
You can also try using their command-line runner end see if your tests work with that. 

http://statlight.codeplex.com
https://github.com/staxmanade/statlight

I'll try to reproduce your issue but I don't have time to spare for the next few days.