Is it possible to write Prism apps such that they can be tested without the UI?

自闭症网瘾萝莉.ら 提交于 2019-12-10 03:54:36

问题


Disclaimer: Prism Novice. I'm reading up furiously to make up for lost time though :)

Context: I need to write automated acceptance tests for a WPF application built using Prism.

Issues: I find that it is convoluted trying to compose the backing ViewModels and everything that they need without the UI.

I may be wrong here... Prism allows you to mark up the shell with named placeholders (regions). Different modules (isolated units) register their Views with the corresponding RegionNames. The Views also have a dependency on the ViewModel (ctor injection) which is injected via MEF/Unity.

  1. Showing the view
  2. triggers creation of child views (regionName => View registry)
  3. triggers creation of child view models (Mef ctor injection).

Composing the app is thus delegated to Prism (or more importantly the View). This seems like a view-first approach. This throws a spanner in the works for starting up the app without the UI ; testing with the UI is a pain.

What I am looking for is a presenter first approach, which composes the entire object (ViewModel and dependencies) graph without the UI.

var viewModel = Someone.ComposeAndGet<ShellViewModel>();

Is it possible with Prism4 by writing apps differently OR is it not supported ?

[Update : Dec 2011]
http://compositewpf.codeplex.com/discussions/283065
Posted on the prism forums to get shed some more light; seems like it is not possible. The recommendation is to use UI tests for acceptance tests. Prism composes UIs ; thereby has a crucial dependency on views.


回答1:


I like the Prism framework, however I rarely use Prism's Regions because I feel it forces me into a View-First approach.

I dislike using View-First because I feel it eliminates some of the advantages of using the MVVM design pattern, such as keeping your layers separate, and unit testing. If your Views are responsible for creating your ViewModels, then to test your ViewModels you either need to create the Views to create the ViewModels, or you need to manually create your ViewModels.

I also feel that an ideal MVVM application should allow you to run the application with any UI, even command-line, and by using Regions I am limiting my ViewModels to a Prism interface.

I'll still use Prism's regions on occasion, but usually only for the startup page (e.g. TitleBarRegion, MenuRegion, ContentRegion)




回答2:


I'm not entirely sure i understand the question, however to Unit test a prism app:

You can use the ServiceLocator to retrieve ViewModels:

MainViewModel mainVM = ServiceLocator.Current.GetInstance<MainViewModel>(); //That will "compose and get"

And then you can unit test it as you please.

That said, Prism, and MVVM in general, delegates most of the responsibilities to the VM. The View is just a representation of the data, it holds no logic.
The VM holds the data/logic and makes the changes to it, without ever knowing which view it is actually bound to.

Hope this helps =)



来源:https://stackoverflow.com/questions/8383495/is-it-possible-to-write-prism-apps-such-that-they-can-be-tested-without-the-ui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!