javascript - Unittesting dependant tests -
i've searched site , literature , couldn't clear answer. i'm trying learn unittesting while constructing new webpage works whiteboard can add post-its.
i have canvas object represents whiteboard, , ticket object represent post-its. have (for now) global function retrieve 1 , canvas, test this:
this.testretrievecanvas = function() { var canvas = getcanvas(); this.asserttrue( canvas != null ); } this.testcanvastype = function() { var canvas = getcanvas(); this.asserttrue( canvas instanceof canvas ); } this.testifcanvasisreused = function() { var canvas = getcanvas(); this.assertequals( canvas, getcanvas() ); }
so, test 3 things:
- does method return canvas?
- is acutal canvas?
- does method give me same canvas always?
no problems far. little later, i'm testing "adding ticket canvas":
this.testaddtickettocanvas = function() { var ticket = factory.createticket("yellow"); var canvas = getcanvas(); canvas.addticket( ticket ); this.asserttrue( canvas.contains( ticket ) ); };
as can see, i'm using getcanvas() function inside test. dependent test now? mean, first 3 tests have pass, if want test able run without doubts. if dependent, how fix this?
strictly speaking, should override getcanvas() return preconstructed (i.e. original constructor isn't called) partial mock of canvas. said, if canvas' constructor empty function , getcanvas method has no business logic involved, shouldn't have problem.
i'd more wary of last 2 statements used together. canvas.addticket( ticket );
ok, since it's function being tested. you're asserting have added ticket using method same object. if method isn't implemented yet, or returns false, or worse true? if addticket method has secondary effects make add ticket list change flag makes contains()
throw exception, or return false, or true? if contains has business logic makes return false ticket sending it, true same ticket in production environment (i.e. test ticket hasn't been initialized, missing state, has been marked excluded environment's business flow), if has no logic now, 2 months project logic changes test fails else works (a new state added, , objects without state deemed non existant, except clients a, b , c). go on.
my point without code can't answer question, give pointers , general answers 1 above. if don't want post code, take consideration of these scenarios, , other scenarios can think of, , if under these scenarios testing code way won't break code nor tests , foreseeable future, you're ok.
Comments
Post a Comment