0

I've just started with unit testing and I'm getting a bit confused and a bit overwhelmed when the subject is testing the models. I currently have some ActiveRecord models with setters and getters. Most of them are simply to encapsulate data, only a few of them bring something new, like for instance a getFullName() method that returns the concatenation of the firstName and lastName fields.

So what should I test? Should I test every single property assignment or should I test only the special methods like getFullName()? To what extent should I go? When should I test the class itself and when should I actually test the database insert? What about the validation already present in the entity, should I take that into account?

What overwhelms me is the amount of tests it seems I would need for an entity with lots of fields. Is there a way around it?

Examples would be specially appreciated!

lbrandao
  • 4,144
  • 4
  • 35
  • 43

1 Answers1

0

Write the tests that you need to give you confidence that your program works properly.

There are people that would argue that if the class has no business logic there is no need to write tests for it. By that argument, then your simple getters and setters don't need to be tested. You only write tests for the parts of the code that have "business logic".

However the other side of the argument is that if you write the code, you should write a test for it. By that argument, you should write a test for them. The test will end up being really trivial. You instantiate the object, set a value with the setter and then assert that the value is then returned with the getter. You end up with a lot of simple tests and it can get tedious to write.

I am in the second camp and have found that if something is hard to test or becomes a pain for testing it is a sign that I have an issue with my design. In this case, you might have accessor methods for things that aren't needed. What likely happened is that you have all your properties in your data object and "need" to have a getter and setter for each of them. But do you really?

In your question, you mentioned that you have a getFullName method. So do you really need methods for getFirstName and getLastName? Why would the data in the object change during the rendering of the page? Why not forgo setters and make the data record 'immutable' so that an update is done by sending a new object to the database for update/create?

I would bet that the reason that you are having this question is that you wrote this code and now are trying to go back and write tests for it. You have this data object and automatically created getters and setters for everything. Then wrote extra methods that you need as you were using it (like the getFullName method). Rather I would recommend that you only create the accessors as you need them. Then you create the test at the time and you will find that the list of functions is substantially smaller. You might have a handful of methods that get the combinations of data that you require as well as a bulk method for getting all the data in a particular form (ie json).

Schleis
  • 41,516
  • 7
  • 68
  • 87
  • I'm starting a new project and I want to start it right. I'm using an ORM with a tool that generates the classes for me. I could have accessors or not. I just thought it's more uniform to have accessors for every property instead of just some. In some parts of the application I might want to use the first name, in others I may want to use the full name. `getFullName` was an example of a custom businesses logic. I will have other methods that will do custom queries and it certainly makes sense for me to test those. I'm just not sure about simple getters and setters. – lbrandao Oct 16 '15 at 19:59
  • I am of the opinion that if you write code it should be worth writing a test for. I was also trying to point out that you don't necessarily need accessors for every property and creating all these methods is extra work. That you trepidation about writing tests for is pointing out to you. – Schleis Oct 16 '15 at 21:33