lundi 6 avril 2015

MVVM unit testing with Jasmine guides

Hello everyone,



I am trying to create an ExtJS 5 application, and it is very important to me to follow a TDD approach.

So I have setup my app, and have setup Jasmine 2.2.0 as well. So i have a sample unit test running after my Ext app loads.

I searched online about ways of unit testing ExtJs5 with Jasmine, but i have not come across examples of a full MVVM setup.

Assume the following scenario:

I have a View, ViewModel, Controller, Models, and Stores.

  • The View binds the ViewModel.

  • The ViewModel links to a Model, and has Stores defined.

  • The Stores and Model have proxies defined that retrieve the data from the back-end server.

  • The Controller has handlers for View events.


So here comes the time where i need to unit test this scenario!

So what i want to unit test for now, is the ViewModel, and the Controller.



Starting from the ViewModel:

  1. Since it is autocreated when we create the view:



  • How do I mock links? The defined link is a Model with an ID, which calls the server to populate the fields. I need this mocked definately!

  • How do i mock stores? These stores have server related proxies as well. I don't want this in my unit tests.


One possibility is to constructor inject the Models/Stores, but then the whole autocreation of the ViewModel by ExtJs is gone. This mean i need to create the ViewModel through my own factory, which will also need to create the View and Controller, and bind them together!



Then we have the Controller:

  1. This is autocreated as well when you create the view.



  • Similarly as the ViewModel mocking question, how do i mock the View and ViewModel that i can access in the controller? Do i constructor inject? If not, then how do i prevent from creating the ViewModel? This will include creating the stores, which will make server calls via their proxies!


I have read that the Sencha Team is using Jasmine internally to unit test their code base, so there must be an answer to these questions. I just might not be obvious to me at the moment.



Any examples, guidelines, links, suggestions, reading material that is not so obvious to google search is much appreciated.





MVVM unit testing with Jasmine guides

Aucun commentaire:

Enregistrer un commentaire