2
 public function addPic($loggedInId,$PicId){

        $chatCoverPhotoObj = new COVER_PHOTO();
        $out = $chatCoverPhotoObj->add($loggedInId,$PicId);

        if($out)
            return true;
        return false;
    }

Above sample code is written in php and simple add record in table COVER_PHOTO Record : "picId" corresponding to loggedin user identified by "loggedInId".

I want know what should i write in unit test of this function. Or for such function we should not write unit test.

Please suggest?

1 Answers1

0

Firsty, if your System-/Class Under Test is using anything that is infrastructural concern -which in your case is the database- that tends to be an integration test.

Unit test are happening in complete isolation with every possible dependencies mocked/stubbed out.

The first problem I see in your code is that you new() up a dependency of your function. By this you can't really easily test your code.

I suggest that you better invert this dependency and have an interface in the constructor of your class, which can receive the concrete implementation of that interface. Once this is done, you can mock out the database part in your test. However by looking at the code you have shared in your question I'm not really convinced what behaviour you are going to test here.

Always make sure what you want to test for, in this case like persistence specification or the mapping configuration (of course in case you are using some sort of an OR/M). Just checking the return value -which is the success or failure flag- of the persistence operation doesn't add much business value to test against.

Community
  • 1
  • 1
kayess
  • 3,384
  • 9
  • 28
  • 45