Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RSpec #13

Merged
merged 1 commit into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ Style/GlobalVars:
- bin/rails7startkit/helpers.rb
- config/initializers/_REDIS.rb
Metrics/BlockLength:
IgnoredMethods: ['describe', 'context']
Exclude:
- config/environments/development.rb
9 changes: 6 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ gem "kaminari", "1.2.2"
gem "whenever", "1.0.0", require: false

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

# Fake data for development and testing
gem "faker", "3.1.0"

Expand Down Expand Up @@ -108,7 +111,7 @@ end

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"
# gem "capybara", "3.38.0"
# gem "selenium-webdriver", "4.7.1"
# gem "webdrivers", "5.2.0"
end
47 changes: 19 additions & 28 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,11 @@ GEM
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
bindex (0.8.1)
bootsnap (1.15.0)
msgpack (~> 1.2)
builder (3.2.4)
capybara (3.38.0)
addressable
matrix
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
chewy (7.2.7)
activesupport (>= 5.2)
elasticsearch (>= 7.12.0, < 7.14.0)
Expand All @@ -101,6 +90,7 @@ GEM
date (3.3.3)
debug (1.7.1)
deep_merge (1.2.2)
diff-lcs (1.5.0)
dry-configurable (1.0.1)
dry-core (~> 1.0, < 2)
zeitwerk (~> 2.6)
Expand Down Expand Up @@ -200,7 +190,6 @@ GEM
net-pop
net-smtp
marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0)
mini_mime (1.1.2)
mini_portile2 (2.8.1)
Expand All @@ -225,7 +214,6 @@ GEM
parser (3.2.0.0)
ast (~> 2.4.1)
pg (1.4.5)
public_suffix (5.0.1)
puma (5.6.5)
nio4r (~> 2.0)
racc (1.6.2)
Expand Down Expand Up @@ -266,6 +254,23 @@ GEM
connection_pool
regexp_parser (2.6.1)
rexml (3.2.5)
rspec-core (3.12.0)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-rails (6.0.1)
actionpack (>= 6.1)
activesupport (>= 6.1)
railties (>= 6.1)
rspec-core (~> 3.11)
rspec-expectations (~> 3.11)
rspec-mocks (~> 3.11)
rspec-support (~> 3.11)
rspec-support (3.12.0)
rubocop (1.44.1)
json (~> 2.3)
parallel (~> 1.10)
Expand All @@ -284,14 +289,9 @@ GEM
rubocop (>= 1.33.0, < 2.0)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
sanitize (6.0.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
selenium-webdriver (4.7.1)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
sidekiq (7.0.2)
concurrent-ruby (< 2)
connection_pool (>= 2.3.0)
Expand Down Expand Up @@ -320,18 +320,11 @@ GEM
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
railties (>= 6.0.0)
webdrivers (5.2.0)
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (~> 4.0)
websocket (1.2.9)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
whenever (1.0.0)
chronic (>= 0.6.3)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.6.6)

PLATFORMS
Expand All @@ -340,7 +333,6 @@ PLATFORMS

DEPENDENCIES
bootsnap (= 1.15.0)
capybara (= 3.38.0)
chewy (= 7.2.7)
config (= 4.1.0)!
debug (= 1.7.1)
Expand All @@ -352,16 +344,15 @@ DEPENDENCIES
puma (= 5.6.5)
rails (= 7.0.4.2)
redis (= 5.0.5)
rspec-rails (= 6.0.1)
rubocop-rails (= 2.17.4)
sanitize (= 6.0.0)
selenium-webdriver (= 4.7.1)
sidekiq (= 7.0.2)
sprockets-rails (= 3.4.2)
stimulus-rails (= 1.2.1)
turbo-rails (= 1.3.2)
tzinfo-data
web-console (= 4.2.0)
webdrivers (= 5.2.0)
whenever (= 1.0.0)

RUBY VERSION
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ Rails 7 Application is Ready in Minutes!
<td><b><a href="https://www.ruby-toolbox.com/categories/code_metrics">Rubocop</a></b></td>
<td>Ruby static code analyzer (a.k.a. linter) and formatter.</td>
</tr>
<tr>
<td><img width="100px" alt="rspec" src="docs/rspec.png"></td>
<td><b><a href="https://www.ruby-toolbox.com/categories/testing_frameworks">RSpec</a></b></td>
<td>Testing Framework for Rails</td>
</tr>
</tbody>
</table>

Expand Down Expand Up @@ -152,11 +157,6 @@ What I'm going to add...
<td><b>Devise and Action&nbsp;Mailer</b></td>
<td>Sending emails for account confirmations</td>
</tr>
<tr>
<td><img width="100px" alt="rspec" src="docs/rspec.png"></td>
<td><b><a href="https://www.ruby-toolbox.com/categories/testing_frameworks">RSpec</a></b></td>
<td>Testing Framework for Rails</td>
</tr>
</tbody>
</table>

Expand Down Expand Up @@ -209,8 +209,9 @@ What was done:
10. Launching Rails App with Puma
11. Launching Sidekiq
12. Rubocop is Checking
13. Visit Rails App: http://localhost:3000
14. Visit Mail Service: http://localhost:1080
13. RSpec is Checking
14. Visit Rails App: http://localhost:3000
15. Visit Mail Service: http://localhost:1080
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Welcome to RAILS 7!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
3 changes: 3 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Article < ApplicationRecord
# ElasticSearch / Chewy
update_index('articles') { self }

# Validations
validates :title, presence: true, length: { minimum: 3 }

# Kaminari. Pagination
paginates_per 3

Expand Down
11 changes: 11 additions & 0 deletions bin/rails7startkit/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# frozen_string_literal: true

require_relative './common'
require_relative './docker'
require_relative './chewy'
require_relative './sidekiq'
require_relative './cron'
require_relative './whenever'
require_relative './rails'
require_relative './puma'
require_relative './rubocop'
require_relative './rspec'

DELAY = 3
ENV_NAME = ENV.fetch('RAILS_ENV', 'development')

Expand Down
9 changes: 9 additions & 0 deletions bin/rails7startkit/rspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Rails7StartKit
class << self
def rspec
container_bash_exec('rails', 'rspec -f documentation')
end
end
end
3 changes: 3 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ module Rails7StartKit
step_info 'Rubocop is Checking'
rubocop

step_info "RSpec is Checking"
rspec

step_info 'Visit Rails App: http://localhost:3000'
step_info 'Visit Mail Service: http://localhost:1080'

Expand Down
18 changes: 18 additions & 0 deletions spec/mailers/demo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

# Preview all emails at http://localhost:3000/rails/mailers/demo_mailer
class DemoMailerPreview < ActionMailer::Preview
# Accessible from http://localhost:3000/rails/mailers/demo_mailer/welcome_email
# Preview all emails at http://localhost:3000/rails/mailers/demo
class DemoPreview < ActionMailer::Preview
# http://localhost:3000/rails/mailers/demo/welcome_email
def welcome_email
DemoMailer.welcome_email
end
Expand Down
45 changes: 45 additions & 0 deletions spec/models/article_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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
69 changes: 69 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] = 'test'
require_relative '../config/environment'
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
abort e.to_s.strip
end
RSpec.configure do |config|
config.before(:suite) do
Chewy.strategy(:bypass)
end

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = Rails.root.join('spec/fixtures')

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

# You can uncomment this line to turn off ActiveRecord support entirely.
# config.use_active_record = false

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, type: :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!

# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
Loading