简体   繁体   中英

How to unit test a WPF application which uses XamlIslands?

I'm developing a WPF application and I'm using Xaml Islands to display win10 UI components. For this I needed to create a UWP project where I can implement those components.

I'd like to write unit tests for my business logic, which is in the WPF project. I can't create a Unit Test App (Universal Windows) , because it's not possible to add as a reference to this project the WPF app. So I tried to create a testing project, which targets .NET Core (tried MSTest Project and xUnit Test Project ), but as I add to the references my WPF app, it shows the following error:

Project <UWP...> is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Project <UWP...> supports: uap10.0.18362 (UAP,Version=v10.0.18362)

This is because the WPF app references the UWP app, to display the win10 components.

So, any idea, how can I add unit testing project to my solution?

So, any idea, how can I add unit testing project to my solution?

There is a design pattern called Model-View-ViewModel (MVVM) which is the recommended design pattern for developing XAML based UI applications. If you are serious about developing UI applications on Windows using XAML, you should really learn it.

One of the main benefits of adopting it, besides the separatation of concerns it brings, is testability. The idea is that you write your application logic in view models that can easily be unit tested in isolation from the rest of the application.

Using this approach, could implement your view model classes in a class library that has little or no dependency upon the WPF or UWP assemblies and then simply add a reference to this class library from your test project as usual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM