No luck Setting Up Async Units Tests

No luck Setting Up Async Units Tests

Old forum URL: forums.lhotka.net/forums/t/11239.aspx


Jav posted on Monday, March 19, 2012

The major promise of MvvM has been the ability to Unit test the code.  But writing code to work with async data access is in the realm of black magic right now.

 I have spent days rummaging around the internet, trying to work with a lot of what appears at first to be quite promising, but in the end produces nothing.  Most of the time the presented code fails to compile with missing references, or otherwise goes nowhere.  What is being offered is more like a teaser than anything else.

MSDN magazine had some very intersting articles, supposedly working with C# 4.0 and VS10, using async and await keywords, but async and await do not even exist in 4.0.  Unfortunately my machine wouldn't let me install the async CTP - everything appears to proceed okay - but after 4 attempts there's no Microsoft VisualStudio Async CTP in MyDocuments folder.

After days of effort, all I have is a single TestMethod in my Silverlight Test Project.  It passes with accolades  because there is no code in it !

I am hoping that somebody has found a way to handle this issue.

Jav

 

sergeyb replied on Monday, March 19, 2012

I used SL Unit Testing Framework that is shipped with VS or SL Tool Kit.  I wrote a number of blog posts on it.  Here is the link to main one.

http://dotnetspeak.com/index.php/2010/07/unit-testing-silverlight-applications-asynchronous-testing/

Jav replied on Monday, March 19, 2012

Sergey, thank you, thank you, thank you.  I have only read the first two paragraphs of the article but it looks very promising. 

Javed

 

RockfordLhotka replied on Monday, March 19, 2012

You can also look at http://unitdriven.codeplex.com - this is the framework we (Justin Chase) created to support unit testing in CSLA so we had the same testing structure on .NET, Silverlight, WP7, and Android. Most importantly, it provides a common model for async test authoring - the rest is largely a clone of nunit/mstest/

Jav replied on Monday, March 19, 2012

Thanks Rocky,

The code certainly seems a lot  simpler.   I have gone through everything on the Codeplex site including each of 7 questions and answers.  I am just having one problem.  In implementing the code in my project I am stuck at the following line:

         UnitTestContext context = GetContext();

I can't figure out where UnitTestContext is defined and where GetContext() is.  I have posted this question at the CodePlex - UnitDriven site.

Thanks for the heads up.

Jav

RockfordLhotka replied on Monday, March 19, 2012

I believe GetContext comes from the base class - your test class needs to subclass a base class from unitdriven.

Jav replied on Monday, March 19, 2012

Right.  I am beginning to understand - I think.  Thank you.

Justin answered my question on CodePlex pretty quickly.

Javed

Jav replied on Monday, March 19, 2012

I have come a long way.  At least my one and only test method is getting executed.  But I am getting an error.  Here is the test method:

       [TestMethod]
        public void GetPersonListTest()
        {
            UnitTestContext context = GetContext();
            BackgroundWorker worker = new BackgroundWorker();
            var vm = new SilverlightApp.PersonViewModel();
            worker.DoWork += (o, e) =>
            {
                vm.SearchingFor = "adam";
                vm.SearchType = 1;
                vm.GetPersonList(true);
            };
            worker.RunWorkerCompleted += (o, e) =>
            {
                var Lst = vm.Model;
                context.Assert.IsNotNull(Lst);

            };
            worker.RunWorkerAsync();

            context.Complete();
        }

The error text is

error {System.UnauthorizedAccessException: Invalid cross-thread access.
   at MS.Internal.XcpImports.CheckThread()
   at System.Windows.DependencyObject.GetValueInternal(DependencyProperty dp)
   at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
   at Csla.Xaml.ViewModelBase`1.get_Model()
   at Csla.Xaml.ViewModelBase`1.SetProperties()
   at Csla.Xaml.ViewModelBase`1.OnSetProperties()
   at Csla.Xaml.ViewModelBase`1.set_IsBusy(Boolean value)
   at Csla.Xaml.ViewModelBase`1.BeginRefresh(String factoryMethod, Object[] factoryParameters)} System.Exception {System.UnauthorizedAccessException}

On the internet, I found someone else asking about a similar error while working with a ViewModel, and someone responded that:

" In order to update a DependencyProperty in a ViewModel, use the same dispatcher you would use to access any other UIElement:

System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => {...}); "

which sounded like Greek to me.  My code in the ViewModel is just one line:

BeginRefresh("GetPersonList", _SearchingFor, _SearchType);

Would appreciate help.

Jav

Jav replied on Monday, March 19, 2012

Looking at my ViewModel code I  though I found the cause of the "invalid cross-thread access".  In my ViewModel, I have the following:

        protected override void OnError(Exception error)
        {
            base.OnError(error);
            System.Windows.MessageBox.Show(error.ToString(), "Person Error", System.Windows.MessageBoxButton.OK);
        }
when I commented out the line for the Messagebox,   that error disappeared.  But when I decided to create multiple breakpoints along the route, I found that the error within the OnError method was still exactly the same.  Besides, the code never reaches the client side  factory method.  If I try to step into the code from my ViewModel, I am instantly at the OnError method above.  I have no problem when running  the program normally.

Jav

Copyright (c) Marimer LLC