WpfViewModelFirst

This project is for researching Actions that need to call an async/await function.

Unit Testing:

This area has been troubling. The MainWindowViewModel needs to set a value of the ObservableObject on the UI thread. Doing a straight unit test will cause a deadlock.

The first process of testing was learning that the Moq mocking framework has the ability to trigger the action passed to Part1ViewModel. This has extra setup and makes the test hard to read.

The next step was finding a way to wait for the ContinueWith in the method to finish. That was accomplished with the TaskCompletionSource. With the code being triggered by the callback, this would exhibit 3 different behaviors. Many times it would pass. Occassionally it would fail one of the asserts. The other times it would frustratingly get into a deadlock.

In searching for a way to test this I learned I can set an attribute on the class to allow internal methods and fields to be seen by the test assembly. This helped the test code be easier to read. [assembly:System.Runtime.CompilerServices.InternalsVisibleTo("WpfViewModelFirst.Tests")]

Using a combination of Task.Run and DispatcherFrame the tests appear to complete successfully every time.

Note: When the Task.Run is awaited the test will deadlock.

Description
Using Actions to continue
Readme 592 KiB
Languages
C# 100%