Testing Frameworks

What's a testing framework?

A testing framework is an execution environment for automated tests. Testing frameworks help teams organize their test suites and in turn help improve the efficiency of testing.

Types of testing frameworks

There are many testing frameworks that work great. Minitest is the default testing framework in Rails 5. However, we will be using the RSpec testing framework instead.

RSpec

How to set up RSpec in Rails

First, create a new Rails app.

rails new testapp

Then, add rspec-rails to both the :development and :test groups in the Gemfile:

group :development, :test do
  gem 'rspec-rails', '~> 3.4'
end

Download and install by running:

bundle install

Initialize the spec/ directory (where specs will reside) with:

rails generate rspec:install

This command will add the spec helper, rails helper, and .rspec configuration files.

Use the rspec command to run your specs:

bundle exec rspec

By default, the above code will run all spec files in the spec directory.

To run only a subset of these specs use the following command:

# Run only a specific folder name
bundle exec rspec spec/folder_name

# Run only specs for a specific type of test such as the post controller
bundle exec rspec spec/controllers/post_controller_spec.rb

RSpec Basics

RSpec.describe Tree do
 it "is able to age by 1 year increments" do
   orange_tree = Tree.new
   orange_tree.age
   expect(orange_tree.age).to eq(1)
 end
end

The 'describe' and 'it' methods come from rspec-core. The Tree class would be from your code. You can think of 'describe' as a header to describe which class you are testing and 'it' as a string/subheader that states what specifically you are testing in the Tree class. Note: you may or may not need the RSpec in front of the describe depending on your RSpec version.

The last line of the example expresses an expected outcome. If orange_tree.age == 1, then the example passes. If not, it fails with a message like:

expected: #< Tree @age=1 >
     got: #< Tree @age=0 >

Matchers

Remember our example in the RSpec Basics section above? The 'to eq' is a matcher! RSpec has many built-in matchers. You can think of them as ways to equate or check certain values or expressions to what you think or expect they would "match" to. In our example, we are saying that we expect orange_tree's age to equal an integer of 1.

Check out the other built-in matchers! https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers

minitest

In case you were curious to see how tests are written in minitest. Minitest also allows you to write tests in 'expectation style' which is very similar to how RSpec tests are written.

class TestTree < Minitest::Test
  def setup
    @orange_tree = Tree.new
    @orange_tree.age
  end

  def test_age_by_one_year_increments
    assert_equal 1, @orange_tree.age
  end
end

To run the test enter the following in Terminal/Command Prompt:

ruby tree_test.rb

Once you run the test this is how the test looks like:

$ ruby tree_test.rb
Run options: --seed 30102

#Running:

. Finished in 0.000980s, 1020.4082 runs/s, 1020.4082 assertions/s. 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

Next Step: