Include me, write test is many people’s nightmare. Many junior programmers feel it is hard to define which should be tested. So, I decided to share my experience after I tech my colleague today.
Before we start talking about how to write a test, let us stop thinking about anything about TDD or BDD or any you may read about it.
And ask yourself, what is “test”? Why we need a “test”?
The target we add the tests to our project usually to prevent human mistake, that means we try to let compute help us to confirm our code matches the “spec”.
But we have to know, the test still is human write code and the spec is human to decide. When you have the wrong spec and wrong way to test, we still get the wrong result.
So, let us try to keep everything simple, and you will feel happy when writing the test.
The Pure Ruby example
In my experience, the test is related to your code. If you have bad code, and you will hard to test it. So, no matter you write the test before implementing anything or after it. The most important thing is the double-check which you want to test and fit your necessities.
Let’s write a
Calculator class, and try to test it.
At first, we have a
Calculator class with initialized
And we can create a simple RSpec skeleton.
And next, let us add the
#add method to the calculator to allow it to add something.
And let’s update our test
In my experience, the best case is you can simply define a
subject which is the target you want to test for, and we can use one line to test it. So I usually try to let my code can be tested like the above example.
In the real world, it may not usually ideal. But this post we didn’t discuss these case, maybe we can discuss in the future.
After we have an imagination about the ideal test, let us try to apply it in the real world.
This morning we are discussing a legacy object which is the order’s payment processor with my team member.
When we try to test this class, we notice it is very hard to add any test for it. Because the information is encapsulation inside the
@payment but we cannot access it.
You may want to expose the
@payment as an attribute like
But if we try to check for the amount is correct, our test code does not make sense.
We test for the “Service Object” not the “Payment Model” inside it. According to this rule, the test should be like below.
At this moment, the “subject” correctly refers to the service’s amount.
Let’s refactor the
PaymentService class to fit our expectations.
After refactoring, the
PaymentService is becoming more straight and we can focus tests on the
This is my experience when I design an object and I usually follow this rule in my work.
More example of Rails
The Rails is the popular framework in Rubyist, I use it almost every workday. How can we use the above skills in Rails?
Just keep your class simple, and everything will be easier to test.
For the model, I usually prevent logic inside it. If your project is small and simple, it is ok to do this. But when your project is complex, we usually have to take several steps to process one thing. And that may be a signal to us to split it into an independent class to focus on this process (we usually call them service object)
If it is possible, I usually try to make my test more simple. It will help us to think about how to design our class is more clear and easier to use.
The above examples only cover very small parts of tests, but I think it is enough to show the suitable test usually depend on your code.
I still not used to write the test before I start work, and I also skip some tests if I have no time to write it.
But according to my experience, even you didn’t write the test you still need to think about “when I try to test my code, which is easier?”
And then, you will notice the best practice we read from the net if we follow it and usually let our code easier to be tested.
For example, the junior will define a method mix different type return values.
It will cause it hard to predict which type will be returned, and we need to write more test cases to confirm it.
This may not an advanced skill, but I spend a lot of years to learn and try to write a test suitable.
And I notice my company’s junior also has the same problem and feeling confusing when I ask them to try to refactor some legacy code.
They feel lost their way and didn’t know where they can start to refactor the code.
So, when you feel confusing, just check for your code about:
- Is the test can focus on my class without depending on others?
- Is my behavior is focused on one thing? (ex. Read and write, validate value, send an API request)
- Is my method returns is expectable? (ex. the only number, object have the same interface)
That sounds very simple and you may read about some object-oriented article about SOLID rules. But it still is hard to design it to a suitable state which didn’t have too many over design.
Anyway, hope my article can help you find some inspiration when you try to write some tests.