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 you start talking about how to write a test, you can stop thinking about anything about TDD or BDD or any you may read about it.
And ask yourself, what is “test?” Why you need a “test?”
The target you add the tests to your project usually to prevent human mistake, that means you try to let compute help you to confirm your code matches the “spec.”
But you 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, you still get the wrong result.
So, you can 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.
You can write a Calculator class, and try to test it.
1class Calculator
2 def initialize
3 @inputs = []
4 end
5end
At first, you have a Calculator class with initialized @inputs array.
And you can create a simple RSpec skeleton.
1RSpec.describe Calculator do
2 let(:calculator) { Calculator.new }
3end
And next, you can add the #add method to the calculator to allow it to add something.
1class Calculator
2 def initialize
3 @inputs = []
4 end
5
6 def add(number)
7 @inputs << number
8 end
9
10 def perform
11 @inputs.sum
12 end
13end
And you can update the test
1RSpec.describe Calculator do
2 let(:calculator) { Calculator.new }
3
4 describe '#add' do
5 let(:number) { 1 }
6 subject { calculator.add(number) }
7
8 it { is_expected.to include(number) }
9 end
10
11 describe '#perform' do
12 subject { calculator.perform }
13
14 before { calculator.add(1) }
15
16 it { is_expected.to eq(1) }
17 end
18end
In my experience, the best case is you can simply define a subject which is the target you want to test for, and you 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 didn’t discuss these case, maybe it can be discussed in the future.
Real-world example
After you have an imagination about the ideal test, you can try to apply it in the real world.
This morning I am discussing a legacy object which is the order’s payment processor with my team member.
1class PaymentService
2 def initialize(payment)
3 @order = payment.order
4 @payment = payment
5 # ...
6 setup
7 end
8
9 def setup
10 @payment.amount = amount
11 @payment.currency = @order.currency
12 # ...
13 end
14
15 def perform
16 return false unless @payment.valid?
17
18 ActiveRecord::Base.transaction do
19 @payment.save
20 VendorAPI.payment.create(amount: @payment.amount)
21 # ...
22 end
23 end
24
25 private
26 def amount
27 @order.items.sum(&:subtotal)
28 end
29end
When you try to test this class, you notice it is very hard to add any test for it. Because the information is encapsulation inside the @payment but you cannot access it.
You may want to expose the @payment as an attribute like service.payment.amount
But if you try to check for the amount is correct, the test code does not make sense.
1subject { service.payment.amount }
2it { is_expected.to eq(100) }
You test for the “Service Object” not the “Payment Model” inside it. According to this rule, the test should be like below.
1subject { service.amount }
2it { is_expected.to eq(100) }
At this moment, the “subject” correctly refers to the service’s amount.
You can refactor the PaymentService class to fit the expectations.
1class PaymentService
2 def initialize(order)
3 @order = order
4 end
5
6 def amount
7 @order.items.sum(&:subtotal)
8 end
9
10 def perform
11 payment = build_payment
12 return false unless payment.valid?
13
14 ActiveRecord::Base.transaction do
15 payment.save
16 VendorAPI.payment.create(amount: amount)
17 # ...
18 end
19 end
20
21 private
22
23 def build_payment
24 @order.payments.build(
25 amount: amount,
26 currency: @order.currency
27 )
28 end
29end
After refactoring, the PaymentService is becoming more straight and you can focus tests on the PaymentService.
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.
1# Model
2RSpec.describe User do
3 it { should validate_presence_of(:email) }
4 # ...
5
6 describe "#avatar_url" do
7 let(:email) { "example@example.com" }
8 let(:user) { create(:user, email: email) }
9 subject { user.avatar_url }
10
11 it "returns Gravatar URL" do
12 digest = OpenSSL::Digest::MD5.hexdigest(email)
13 should eq("https://www.gravatar.com/avatar/#{hash}")
14 end
15 end
16end
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, you usually have to take several steps to process one thing. And that may be a signal to you to split it into an independent class to focus on this process (commonly called service object)
1# Request
2RSpec.describe "/api/users", type: :request do
3 describe "GET /api/users" do
4 let(:users) { create_list(:user, 5) }
5 before { get api_users_path }
6 subject { response.code }
7 it { should eq("200") }
8
9 describe "body" do
10 subject { JSON.parse(response.body) }
11 it { should_not be_empty }
12 # ...
13 end
14 end
15end
If it is possible, I usually try to make my test more simple. It will help you to think about how to design the 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 you try to test your code, which is easier?”
And then, you will notice the best practice you read from the net if you follow it and usually let your code easier to be tested.
For example, the junior will define a method mix different type return values.
1def sum
2 return false if summable?
3
4 @items.sum
5end
It will cause it hard to predict which type will be returned, and you need to write more test cases to confirm it.
Conclusion
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.