Unit Test upon methods that use UI elements?

I am not even sure how to world this right. I have an Universal Windows Application that is currently on the way. I write some Unit Test using MSTest along the way, and I ran into this problem.

I have a method that use a Writeablebitmap from Windows.UI.XAML namespace, to convert some of the image object to something that the code would understand, which is totally not UI related at all. But the unit test would fail with this error:

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)). If you are using UI objects in test consider using [UITestMethod] attribute instead of [TestMethod] to execute test in UI thread.

This is where the test fail:

 Windows.UI.Xaml.Media.Imaging.WriteableBitmap iconWriteableBitmap = new Windows.UI.Xaml.Media.Imaging.WriteableBitmap(1, 1); 

I searched around, and it seems that the code tries to access something on the UI Thread from another thread. Which is something that does not suppose to happen. This method/line works in the context of the app running normally, but the unit test will fail.

I tried to write the Attribute [UITestMethod] on top of the test method but Intellisense reports that it can't find such attribute. So as of it right now, i have no mean of testing that function rather than actually deploy the app and test it manually.

I googled around and there was an article that showing this problem with an older version of Windows 10 SDK, but claimed that it was fixed on one of the recent version. I updated everything in term of tools and SDKs and still getting this. Running VS 2015. Any help would be great.

by AMRAAM_Missiles via /r/csharp

Leave a Reply