質問

In our main Silverlight business app, we have a lot of controls that are visible and/or enabled based on user roles (e.g. Admin menu link visible only if in Admin role). We control that visibility via converters.

I'm just starting out writing unit tests, and I'd like to be able to test the visibility of these controls based on different roles (it'd be really bad if we had a bug there). However, since we're using the MVVM pattern, and the controls are not named, I'm not sure how to go about that. Any thoughts?

役に立ちましたか?

解決

This is exactly what ViewModel is for. You should be able to test your view models easily (in your case I suppose whether they expose correct roles to view). And since converter is just a regular class, they should be tested just as well.

In terms of unit testing, you really got just two tasks to do:

  1. Test whether view models expose correct data
  2. Test whether converters convert role to visibility correctly

Here's where unit testing ends. Those tests should guarantee your view is fed with appropriate data to render itself as you planned. Now whether it does is different thing, but that's out of scope of unit testing. The final testing belongs to acceptance testing, quality assurance and also automated/scripted UI tests and more often than not - human verification.

他のヒント

I don't think that naming controls breaks MVVM in any way. If you are fundamentally opposed to naming them you can probably use the VisualTreeHelper to walk the control tree and find the controls you are interested in.

You can use Moq to mock the viewmodels to simulate specific roles and Microsoft's Silverlight testing framework to run the tests.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top