[General boards] [Winter 2019 courses] [Fall 2018 courses] [Summer 2018 courses] [Older or newer terms]

Call method vs. use protected fields in tests


#1

In general is it better to call methods such as fish.getX() or to directly access protected fields such as fish.x in tests? Thanks!


#2

It’s better to use .getX() if it exists, but I wouldn’t make a “.getAppearance()” method solely to be referenced in a test, for example


#3

Thanks! So are we supposed to test whether appearance changes as well?


#4

No, that was just an example – strictly speaking, you just need to test that the listed behaviors work as they should, and none of the behaviors mention appearance.


#5

Thanks!! Also, while we have to call setEntity() for any mocked objects, I am just wondering for the object that we are actually testing (ex. the main fish in FishTest), when should we call setEntity(x, y, fish)/getEntity(x, y) and when can we simply use setLocation(x, y)/getX()/getY()? Thank you!


#6

If you are testing that a fish doesn’t collide with bubbles (this is a really important test!), you’ll need to add a bubble to the entities array (otherwise, how would the fish know where to look for it?)

In that case (e.g., collisions & seaweed eating), you’ll need to put mock entities in the actual grid so that they can be “discovered” by the entity you are actually testing

In the FollowingFish test, we didn’t need to add the “followee” into the entities array, because the following fish “learned” about it through its constructor – consider how this is different from collisions


#7

So if the method that we are testing does not access the entities array, we can just use setLocation but it would still be okay if we use setEntity() in this case as well right?


#8

I guess, just unnecessarily complicated