Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

What is the MVVM design pattern? how does it enable better unit testing?

The Model-View-ViewModel design pattern claims to aid separation of the View and the Business Logic in the GUI. What is it all about and how does it achieve separation of the GUI to enable better unit testing?

user-image
Question ajoutée par Daanish Rumani , Product Manager , Publicis Sapient
Date de publication: 2013/07/27
Thajudeen Anwar Basha
par Thajudeen Anwar Basha , Technical Specialist , Philips

Here is the example how it is easy to do unit test using MVVM Prerequisites : Mocking frameworks example Rhino.Mock or Moq from Microsoft.
Because in MVVM model most probably you will be working with .Net Interface means expose the public function and implement in respective classes.
For example.
Consider you have UI(xaml) which has a textbox and button.
when you press the button the entered text should be processed.
Below is the skeleton.== with respect MVVM - UI -> ViewModel -> Model (i.e Controller) UI.xaml Enter property from ViewModel is binded to textbox control property "Text" EnterCommand property is binded to button control "Command" ViewModelInterface: public Interface IViewModelFace { string EnterString { get } ICommand EnterCommad { get } } ViewModelClass: //Note VimeModelClass should be inherit from ViewModelBase you can google about this class.
public class ViewModelClass : ViewModelBase, IViewModelInterFace { //Ctor public ViewModelClass(IControllerClass controllerClass) { mControllerClass = controllerClass; } public string EnterString { get set } private ICommand mEnterButtonCommand; ICommand EnterButtonCommand { if(mEnter == null) { mEnter = new RelayCommand(para => Enter()); } return mEnterCommand; } private void Enter() { //When you press the button from ui this particular command should executes and //call the model(Controller) class to store in db.
//because all the busines logic needs written in Model //ViewModel should only facilitate the behavior of the UI no logic.
mControllerClass.Save() } } Now comes the unit testing part.1.
First thing you need to know what you gonna test.
Here you need to the "EnterButton".
Bu using MVVM model we exposed the commands and property in an interface which gives you the freedom to do unit test.2.
What needs to Mocked (Mocked object is something you defining the behavior) Here comes you test case // public class TestVeiwModelClass { provate IControllerClass mController private ViewModelClass mViewModelClass [Setup] public void Init() { //Create the mock object mController = MockRepositery.GenerateMock(); mViewModelClass = new ViewModelClass(mController); } } [Test] public void EnterButton_Scenario_Expectation() { // Here you are executing the button ("EnterButtonCommand") mViewModleClass.EnterButtonCommand.Execute(null) //When the command is fired your expectation is whether the model is called are not mController.AssertWasCalled(x=>x.Save()); } This is how you can test unit by using MVVM pattern also it is very easy to do SystemTesting (Automating the system).
Hope this gives some light on unit testing.
If you need further details on unit testing you post your question.
Regards, Thaj

Utilisateur supprimé
par Utilisateur supprimé

http://en.wikipedia.org/wiki/Model_View_ViewModel

More Questions Like This