If you're beginning to write code, perhaps for a personal project, school project, or a bootcamp, you'll need to ensure that your code is reliable and works as expected.
But how do you go about testing your code?
Do you open up a console such Ruby’s IRB and manually type your code, do you paste it, or perhaps import your files? Or perhaps you insert breakpoints with a tool such as Pry, and then manually test it. Or perhaps you run the file with Ruby, instantiate instances, and test some methods.
All of these might work, but they are also quite tedious and involve a large amount of repetition. DRY, or don’t repeat yourself, is a principle of software engineering, and a good habit to get into.
You may have considered writing automated tests, and have even looked into test suites such as Minitest or RSpec. but have been put off by their learning curve of verbosity.
Consider this hello_world method that (implicitly) returns the string 'Hello world":
def hello_world 'Hello world' end
And here is how to test it with RSpec:
require 'rspec' describe hello_world do it 'returns the string "Hello world"' do expect(hello_world).to eq "Hello world" end end
That's six lines and over 100 characters to test a simple hello statement! Besides, what exactly are words like 'describe', 'it', 'to', and 'eq' anyways. I haven't seen those in Ruby.
Do I really need to learn a whole new test suite and language (DSL) to ensure that my project functions as intended? And if I my project contains 20 methods, will I have to write another 100 lines of code to test it?
It is at this point that many beginners will unfortunately abandon automated testing. But that doesn’t need to be the case. There is a simpler solution, once which will allow you to improve your understanding of Ruby, without having to spend all that extra time learning a test suite.
While test suites such as RSpec may be necessary for commercial grade software, Ruby comes out of the box with much of the power and flexibility you’ll need to write your own tests. And not only can you create your own tests using the knowledge you already have of Ruby, but you can do it your own style--unencumbered by the additional semantics of test-suites.
As is usually the case, there are many ways you can write your tests. Let’s look at several ways of testing the above method using core Ruby, beginning with a simple one line equality test:
hello_world == 'Hello world'
This will return true if it passes, and false if it fails. Calling your methods is particularly important as you may otherwise not be aware if they contain syntactical errors.
You can write if/else statements, and return or just output any code you like:
if hello_world == 'Hello world puts 'pass' else puts 'failed' end
Or if you prefer, you can use the ternary operator:
puts hello_world == 'hello world' ? 'pass' : 'fail'
You can if on a specific condition:
fail if hello_world != 'Hello world'
You can also use 'raise' with a custom error message:
raise 'hello_world producing unexpected output' unless hello_world == 'Hello world'
Here are some reasons to begin writing your tests in Ruby:
- Save time. You can write your test in just one line--if that's your style. And then you can have them run again and again without having to re-enter them into a console every time you change your code.
- You get to learn about and implement many new Ruby methods, iterators, and enumerators.
- Understand exactly what each part of your program returns. Is is a string? Is it an array? Might it sometimes be nil?
- Familiarize yourself with the concepts and thinking patterns of Test driven development .
- Best of all, no need to burden yourself by learning another language and technology. Focus on Ruby itself, and once your comfortable with it, move on to implementing what you've done using test suites such as Minitest, RSpec, and the others you'll learn when your projects demand them.