ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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
تم إضافة السؤال من قبل Daanish Rumani , Product Manager , Publicis Sapient
تاريخ النشر: 2013/07/27
Thajudeen Anwar Basha
من قبل 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

Ahmad Anbari
من قبل Ahmad Anbari , Software System Engineer , Continental Jet Services FZCO

The view model in the MVVM pattern exposes both properties for the data to be displayed in the UI and operations on the data that can be invoked from the UI. An MVVM view model has no notion that a view (or any specific UI technology) exists. An MVVM view uses the binding feature to bidirectionally associate properties exposed by controls in the view (items in a drop-down menu) with the properties exposed by the view model.

 

With the concept that the model/view has not notion about each other, it is really easy to write a unit test.

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟