tags:

views:

22

answers:

4

Why is the unit test useful when the view is not unit testable in MVVM?

In MVVM, we have the models, view-models, and views. The claimed advantage is that MVVM can make the models and view=models unit testable. But all the three parties belong to the same application. If the views are not unit testable, why test the other two? Will unit testing the other two and leave one not tested improve the quality?

Removing all the code-behind from the views sounds weird to me. How about the code-behind only handles the pure UI operations?

A: 
  1. Views in MVVM are stupid, you give them data they show it to the user, done.
  2. Actions are restful, you can test that in unit tests.
  3. You can test what view is used for a route
Perpetualcoder
"what view is used for a route" ? Aren't you confusing MVVM with ASP.NET MVC ?
Thomas Levesque
well.. some times you do not want to expose the model classes ( for eg: if you use Linq2SQL ), so the views work with the specialized view models.here you have a hybrid M-V-VM-C
Perpetualcoder
A: 

Part of the idea is certainly that your view should be so trivial that it "doesn't need" to be unit tested. However, it is possible to unit test GUIs, see http://www.c2.com/cgi/wiki?TestFirstUserInterfacesPrinciples

dash-tom-bang
A: 

Once you adopt MVVM you would find that your views become very very light and a lot of the logic moves into the viewmodel. The "pure UI operations" is more of an imagined threat than a real one.

Amongst those things that are in the view but are not unit testable are animations and bindings. Using declarative forms (i.e. XAML) for both helps in reducing the overall instability of the system. Once those work, they continue to work. Besides, taking the app logic and separating it from UI "sugar" goes a long way in ensuring that your changing app logic does not destabilise the UI logic.

Besides, if you have a really complex animation in code, there's no stopping you from abstracting it in a unit testable way.

Igor Zevaka
I agree the Idea is to push 'Logic' into a separate class, where tests can be performed on it, with out the cumbersome view. Another valid use is the ability to swap out 'Behavior' for the view seamlessly. You may have a view that is fairly generic, that you want the behavior of to change, depending on the context. You could pack that logic into the code behind, using a state field, however swapping out a ViewModel would be cleaner.
Agies
There are many places that we can make mistakes in the views:1. Triggers are actually program logic.2. If we put the binding path wrong, we will not receive any run-time error, but the view will simply not display the correct data.3. If we bind the command to a wrong handler in the view-model, the program will not work as desired.All these need to be tested. But if we only test the view-model, the program is still not tested. What is the benefit then?
BigTiger
A: 

There are many places that we can make mistakes in the views: 1. Triggers are actually program logic. 2. If we put the binding path wrong, we will not receive any run-time error, but the view will simply not display the correct data. 3. If we bind the command to a wrong handler in the view-model, the program will not work as desired. All these need to be tested. But if we only test the view-model, the program is still not tested. What is the benefit then?

BigTiger