Rails 7. Start Kit loves RSpec!

Ilya Zykin
3 min readFeb 4, 2023

--

Rails 7. Start Kit loves RSpec!

Rails 7 Start Kit — Dockerized Rails App with the most popular preinstalled tools.

BTW

This week me and the project few days were in the top of Github trends

The Story

In this release I introduce RSpec for Rails. Not so big difference with minitest in order to popularity and an amount of downloading, but almost everywhere where I worked RSpec was a standard de-facto.

There is PR that introduces RSpec. Now let me tell you what I have done.

How I did it

Step 0

Removed a folder with minitest specs

rm -rf test

Step 1

I’ve added rspec-rails gem in Gemfile

group :development, :test do
# RSpec testing
gem "rspec-rails", "6.0.1"

...
end

Step 2

I found that I do not use (at least now) some testing dependencies and commented them out in Gemfile

group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
# gem "capybara", "3.38.0"
# gem "selenium-webdriver", "4.7.1"
# gem "webdrivers", "5.2.0"
end

Step 3

bundle install

Step 4

To initialise rspec you need to run

rails generate rspec:install

Folder structure and some important files were created.

Step 5

I use Chewy gem to provide ElasticSearch indexing of my models. Because of that I need to modify some rspec files.

In spec/spec_helper.rb and spec/rails_helper.rb I need to add the following:

RSpec.configure do |config|
config.before(:suite) do
Chewy.strategy(:bypass)
end
...
end

Step 6

To check specs I modified a bit the only model in my project app/models/article.rb

class Article < ApplicationRecord
...
# Validations
validates :title, presence: true, length: { minimum: 3 }
...
end

Step 7

Now I can write some tests for my Model

spec/models/article_spec.rb

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Article, type: :model do
it 'creates an article' do
Article.create(
title: 'ABC',
content_raw: 'test content'
)
expect(Article.count).to eq(1)
end

it 'processes content_raw in content' do
article = Article.create(
title: 'Article title',
content_raw: '<h1>test content<h1>'
)
expect(article.content).to eq('test content')
end

context 'negative cases' do
it 'fails if title is of whitespaces ' do
article = Article.create(
title: ' ',
content_raw: '<h1>test content<h1>'
)
expect(
article.errors.messages[:title]
).to eq(["can't be blank"])
end

it 'fails if article title length less than 3 symbols' do
article = Article.create(
title: 'AB',
content_raw: 'test content'
)
expect(
article.errors.messages[:title]
).to eq(['is too short (minimum is 3 characters)'])
end
end
end

Step 8

Also I did some simple tests for Mailer. I just followed documentation for RSpec

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe DemoMailer, type: :mailer do
let(:mail) { DemoMailer.welcome_email }

it 'renders the headers' do
expect(mail.subject).to eq('Welcome to Rails 7. StartKit')
expect(mail.to).to eq(['test@test.com'])
expect(mail.from).to eq(['demo@rails7startkit.com'])
end

it 'renders the body' do
expect(mail.body.encoded).to match('Welcome to Rails7. StartKit')
expect(mail.body.encoded).to match('Thanks for using this project!')
end
end

Step 9

On the final step I just did some improvements in scripts of my project to make it run easily

# frozen_string_literal: true

module Rails7StartKit
class << self
def rspec
container_bash_exec('rails', 'rspec -f documentation')
end
end
end

Step 10

And now when you run bin/exec rspec from the host machine you can see the following

Happy Coding!

Happy coding and tell your friends about this project 👍

--

--

Ilya Zykin
Ilya Zykin

Written by Ilya Zykin

IT coach. React and Rails enthusiast. Passionate programmer, caring husband and pancake baker on Sundays. School teacher of computer studies in the past.

Responses (1)