php - Is Unit Test Applicable Here? -
public function addpic($loggedinid,$picid){ $chatcoverphotoobj = new cover_photo(); $out = $chatcoverphotoobj->add($loggedinid,$picid); if($out) return true; return false; }
above sample code written in php , simple add record in table cover_photo record : "picid" corresponding loggedin user identified "loggedinid".
i want know should write in unit test of function. or such function should not write unit test.
please suggest?
firsty, if system-/class under test using infrastructural concern -which in case database- tends integration test.
unit test happening in complete isolation every possible dependencies mocked/stubbed out.
the first problem see in code new()
dependency of function. can't test code.
i suggest better invert dependency , have interface in constructor of class, can receive concrete implementation of interface. once done, can mock out database part in test. looking @ code have shared in question i'm not convinced behaviour going test here.
always make sure want test for, in case persistence specification or mapping configuration (of course in case using sort of or/m). checking return value -which success or failure flag- of persistence operation doesn't add business value test against.
Comments
Post a Comment