MyGit

v1.5

the-teacher/rails7-startkit

版本发布时间: 2023-02-05 03:18:04

the-teacher/rails7-startkit最新发布版本:v2.1(2023-09-18 15:15:47)

Rails 7. Start Kit loves RSpec!

Rails7 StartKit-1 5

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

Great if you want to:

👉 Subscribe to the project to know about most recent updates.

RELEASE 1.5 DETAILS

In the top of GITHUB trends

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

Screenshot 2023-02-02 at 16 04 34

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.

Screenshot 2023-02-04 at 21 54 58

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

Screenshot 2023-01-29 at 23 37 11

Happy Coding!

Happy coding and tell your friends about this project 👍

相关地址:原始地址 下载(tar) 下载(zip)

查看:2023-02-05发行的版本