untymage's avatar

Should i write a test for each model which have polymorphic relationship?

So if i set up polymorphic relation between comment vsThread and Video etc.. Should i write a test for every model which haveMany comments ? for example

public function a_user_can_comment_on_video (){

    //same code
}

public function a_user_can_comment_on_thread (){

    //same code
}
0 likes
3 replies
ftiersch's avatar

I'd say it depends on your code around it. You usually don't need to test basic Eloquent functionality - the Laravel team has tests for that. But if you have certain functionality around it you can definitely test that.

Also depends of course on how "sure" you want to be :) If you think "well, if it works for video it should work for threads" thats fine. If you want to be absolutely sure you can write a test.

1 like
mstrauss's avatar

@untymage

I can honestly say I have never regretted writing a test. On the other hand, many developers, myself included can tell you horror stories about tests that should have been written. I would suggest that you definitely write the test. It will only take a few seconds/minutes (dependent on the complexity) and your app will be that much more solid giving you (and other future devs that work on the project) confidence to refactor without fear of breaking things. This is especially true with polymorphic relations as they are more complex than the average relationship and people will be less likely to refactor related items for fear of breaking them.

Please or to participate in this conversation.