DEV Community

Cover image for Testing, 1, 2 : Basic Testing in Rails
Eben Eleazer
Eben Eleazer

Posted on

Testing, 1, 2 : Basic Testing in Rails

When creating a new Rails app or API using rails new, we get a lot of files and directories set up for us. Until now, I'd been avoiding one folder in particular.

Test

rails new generated file structure

It's time to tackle (1) why you would write tests and (2) how to write basic tests in Ruby on Rails.

See, it wasn't just a joke

1. Why Write Tests

You may be thinking, "why would I write a test, when I already know how to pass it", or something like that. Especially for personal projects or when you're the only person working on a particular codebase, writing tests may seem tedious or unnecessary.

But, I'm going to give you a few reasons why Test Driven Development can be helpful, even for personal projects.

Helps Application Design

Outcome decided gif

By writing tests first it forces you to think of the outcome of your code. You decide what is expected, then you write code to achieve it. No matter if the test is behavior driven or directly checks for a specific return value, the fact that you are forced to make these decisions beforehand, can keep you from developing unnecessary code.

Makes Refactoring Painless

When you have tests, you can refactor your code without worrying (too much) about unaffected side effects. Since you wrote tests for all the important functionality, then as long as the tests still pass you should be good.

2. How to Write Tests

Okay, so now that you've decided it's worthwhile, let's write some tests!

cat mick check gif

The Test folder

In the root of your Rails app (generated with rails new) a "test" folder will be created. When you inspect this folder you'll see a similar set of subfolders to our "app" folder.

There are a couple key differences between the test and app directories, but for the most part our test files, and thier paths within the "test" folder will correspond directly with thier counterparts in our "app" folder.

More concisely, the file at "/test/models/model_test.rb" will contain the test for "/app/models/model.rb"

In fact, when you use rails generators or rails g [model or whatever], a test class will also be generated and placed in the corresponding test folder.

Test Syntax

A test contains at least one assertation. An assertation evaluates an object or expression for expected results. There are quite a few different assertations you can use, depending on what you are trying to test, and a full list is available here.

Here is the most basic test case:

test "the truth" do
  assert true
end
Enter fullscreen mode Exit fullscreen mode

Which is technically the same as this:

def test_the_truth
  assert true
end
Enter fullscreen mode Exit fullscreen mode

But it doesn't look as cool (plus the first syntax makes it clear it is a testing method), so don't use the second version.

Here is an example of a test for a model validation on email address

 test 'does not accept bad email addresses' do
    user = User.new(email: "not an email address", password_digest: 'password')
    user_2 = User.new(email: "notanemailaddress", password_digest: 'password')
    user_3 = User.new(email: "anemail@addr.ess", password_digest: 'password')

    assert_not user.save, "email should not allow spaces"
    assert_not user_2.save, "email should have an @ and a ."
    assert user_3.save
  end
Enter fullscreen mode Exit fullscreen mode

Notice that a single test can contain multiple assertations. The test will not pass until all assertations are successful.

More about tests

What I've talked about are the basics of testing. I may do a future piece on different types of test (Model, System, Integreation, Functional) but for now, I hope I've given a general overview of testing in rails

Top comments (0)