Can't access ViewModelBase.Model from test class

Can't access ViewModelBase.Model from test class

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


JonM posted on Wednesday, April 03, 2013

I'm using the MVVM pattern for a WPF application.  Inside my viewmodel I make an async await call.  Then manipulate my Model.  Everything works perfectly inside my WPF app.  In my test class this totally fails.  The await if coming back on a different thread.  I get a 'you can't access this because it is owned by a different thread' error.  How do I make it use the calling thread for a callback?  I've tried ConfigureAwait with true and false with no luck.

RockfordLhotka replied on Wednesday, April 03, 2013

This is a common issue with unit testing and async operations.

Unit testing frameworks typically don't create a synchronization context, and UI technologies do. So code behaves differently in a unit test environment than in a real UI environment.

If you search the web I suspect you'll find a number of solutions, probably involving the creation of a custom synchronization context object to get the same behavior in your unit test environment as in a real UI environment.

Copyright (c) Marimer LLC