How to test code in Ruby on Rails?

Robert - testing, minitest, rspec, cucumber, factorybot, faker, guard, simplecov
Shared on

A couple of options:

  • minitest
  • rspec
  • cucumber

Initial research:

Why decide for minitest or rspec? Asked this question on reddit.

Okay, so minitest seems to be the "lighter and faster" option. If I need more complex things, I'll consider rspec. I'll start by looking at minitest.

No real how to get started or setup guide. So I search for how to setup minitest.

Best results:
https://stackoverflow.com/questions/6715158/how-to-set-up-minitest
https://chriskottom.com/blog/2014/10/4-fantastic-ways-to-set-up-state-in-minitest/
https://blog.codeship.com/getting-started-with-minitest/

I want it easier so I also search for tutorial minitest.

Best results:
https://semaphoreci.com/community/tutorials/getting-started-with-minitest
https://launchschool.com/blog/assert-yourself-an-introduction-to-minitest
https://gist.github.com/yasinishyn/b09a8d84a21bed5106f0
https://mattbrictson.com/minitest-and-rails

When you install the current version (9 may 2019) of minitest you get an error.
So, add gem 'minitest', '5.10.3' and do bundle install or bundle update minitest instead.

Run rails test and see if you need to debug things (I had to).

I went for a test to see if I get a 200 statuscode on my root_url, created the file test/routes/homepage_test.rb:

require 'test_helper'
require 'minitest/autorun'

class HomepageTest < ActionDispatch::IntegrationTest
  test "should get home" do
    get root_url
    assert_response :success
  end
end

Running rails test gets:

Running via Spring preloader in process 12863
Run options: --seed 3496

# Running:

.

Finished in 0.767240s, 1.3034 runs/s, 1.3034 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

Hello world! It works.

Getting more specific tests in place is necessary. Starting with testing the CRUD of tips.

Added faker, factory-bot-rails, guard, guard-minitest and simplecov to the Gemfile.


Tweet