actionscript 3 - How can unit tests be modular when they are dependant on other units? -


i trying retrospectively unit test application complex utilises mvc. know retrospectively applying unit tests isn't ideal still believe it's possible refactoring existing code. majority of time not possible unit test 1 unit, without relying on other units i.e. view relies on model.

what best way unit test in case? better utilise real model or create mock model?

the problem utilising real model in situation model relies on other response classes data xml there's chain of reliance. model has lot of data easier use maybe i'm missing point.

i have provided uml of application brevity.

enter image description here

**edit ****

ok if correct, best practice create mock data inside mock class? example have mock class "mockplaylistpanelmodel" creates data required view class "playlistpanel" run without errors:

class mockplaylistpanelmodel extends mock implements iplaylistpanelmodel {   /**     * return playlist items    * @public     */   public function mainplaylistitems():vector.<playlistdata>    {     var playdata:vector.<playlistdata> = new vector.<playlistdata>;     var playlistresp:playlistdata = new playlistdata(0, "", "", 0, 0, 0, 0);     playdata.push(playlistresp);     return playdata;    }  } 

to retrospectively fit unit testing existing application, need change application code support unit testing (as rightly mention may need perform refactoring). of course risk here changes application introduce bugs, cannot protected against without having tests in place.

therefore, sensible approach system level tests in place covering of key use cases. acts kind of 'test scaffolding' around application, meaning can more safely begin introduce lower level tests decreased risk of introducing bugs modify application make more testable. once in place, can introduce policy developers must write tests around code change before change - allows organically grow set of automated tests around application.

i highly recommend getting hold of working legacy code - excellent book covers sorts of useful techniques introducing testing existing application has little automated testing.

regarding question on whether should create mock data inside mock class testing, 1 approach can take when injecting test versions of objects, not best. using mocking framework mockito can create mock objects defined behaviour on fly. in case, can use mockito create mock model implementation, , inject mock model whatever object depends on it.


Comments