Skip to content

Commit 1212745

Browse files
test: improve user tests
1 parent 748d294 commit 1212745

12 files changed

+110
-27
lines changed

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ gem 'rack-cors', '1.1.1'
6262
group :development, :test do
6363
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
6464
gem "debug", platforms: %i[ mri mingw x64_mingw ]
65+
gem 'factory_bot_rails', '6.2.0'
66+
gem 'ffaker', '2.20.0'
6567
gem "rspec-rails"
6668
gem "rubocop-rspec"
6769
end
@@ -79,6 +81,7 @@ end
7981

8082
group :test do
8183
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
84+
gem 'database_cleaner-active_record', '2.0.1'
8285
gem "capybara"
8386
gem "selenium-webdriver"
8487
gem "webdrivers"

Gemfile.lock

+21-8
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ GEM
9797
childprocess (4.1.0)
9898
concurrent-ruby (1.1.9)
9999
crass (1.0.6)
100+
database_cleaner-active_record (2.0.1)
101+
activerecord (>= 5.a)
102+
database_cleaner-core (~> 2.0.0)
103+
database_cleaner-core (2.0.1)
100104
debug (1.4.0)
101105
irb (>= 1.3.6)
102106
reline (>= 0.2.7)
@@ -122,6 +126,12 @@ GEM
122126
dry-core (0.7.1)
123127
concurrent-ruby (~> 1.0)
124128
erubi (1.10.0)
129+
factory_bot (6.2.0)
130+
activesupport (>= 5.0.0)
131+
factory_bot_rails (6.2.0)
132+
factory_bot (~> 6.2.0)
133+
railties (>= 5.0.0)
134+
ffaker (2.20.0)
125135
ffi (1.15.5)
126136
globalid (1.0.0)
127137
activesupport (>= 5.0)
@@ -170,25 +180,25 @@ GEM
170180
net-protocol
171181
timeout
172182
nio4r (2.5.8)
173-
nokogiri (1.13.2-aarch64-linux)
183+
nokogiri (1.13.3-aarch64-linux)
174184
racc (~> 1.4)
175-
nokogiri (1.13.2-arm64-darwin)
185+
nokogiri (1.13.3-arm64-darwin)
176186
racc (~> 1.4)
177-
nokogiri (1.13.2-x86_64-linux)
187+
nokogiri (1.13.3-x86_64-linux)
178188
racc (~> 1.4)
179189
orm_adapter (0.5.0)
180190
parallel (1.21.0)
181191
parser (3.1.1.0)
182192
ast (~> 2.4.1)
183-
pg (1.3.2)
193+
pg (1.3.3)
184194
public_suffix (4.0.6)
185195
puma (5.6.2)
186196
nio4r (~> 2.0)
187197
racc (1.6.0)
188198
rack (2.2.3)
189199
rack-cors (1.1.1)
190200
rack (>= 2.0.0)
191-
rack-mini-profiler (2.3.3)
201+
rack-mini-profiler (2.3.4)
192202
rack (>= 1.2.0)
193203
rack-test (1.1.0)
194204
rack (>= 1.0, < 3)
@@ -254,8 +264,8 @@ GEM
254264
rubocop-ast (>= 1.15.1, < 2.0)
255265
ruby-progressbar (~> 1.7)
256266
unicode-display_width (>= 1.4.0, < 3.0)
257-
rubocop-ast (1.15.2)
258-
parser (>= 3.0.1.1)
267+
rubocop-ast (1.16.0)
268+
parser (>= 3.1.1.0)
259269
rubocop-rails (2.13.2)
260270
activesupport (>= 4.2.0)
261271
rack (>= 1.1)
@@ -277,7 +287,7 @@ GEM
277287
actionpack (>= 5.2)
278288
activesupport (>= 5.2)
279289
sprockets (>= 3.0.0)
280-
stimulus-rails (1.0.3)
290+
stimulus-rails (1.0.4)
281291
railties (>= 6.0.0)
282292
strscan (3.0.1)
283293
thor (1.2.1)
@@ -323,9 +333,12 @@ DEPENDENCIES
323333
brakeman
324334
bundler-audit
325335
capybara
336+
database_cleaner-active_record (= 2.0.1)
326337
debug
327338
devise (= 4.8.1)
328339
devise-jwt (= 0.9.0)
340+
factory_bot_rails (= 6.2.0)
341+
ffaker (= 2.20.0)
329342
image_processing (~> 1.2)
330343
importmap-rails
331344
jbuilder

config/environments/development.rb

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require 'active_support/core_ext/integer/time'
44

5+
Rails.application.routes.default_url_options[:host] = 'localhost:3001'
6+
57
Rails.application.configure do
68
# Settings specified here will take precedence over those in config/application.rb.
79

config/environments/test.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# your test database is "scratch space" for the test suite and is wiped
88
# and recreated between test runs. Don't rely on the data there!
99

10+
Rails.application.routes.default_url_options[:host] = 'localhost:3001'
11+
1012
Rails.application.configure do
1113
# Settings specified here will take precedence over those in config/application.rb.
1214

@@ -38,7 +40,9 @@
3840
# Store uploaded files on the local file system in a temporary directory.
3941
config.active_storage.service = :test
4042

41-
config.action_mailer.perform_caching = false
43+
config.action_mailer.default_url_options = { host: 'localhost', port: 3001 }
44+
45+
config.action_mailer.perform_caching = true
4246

4347
# Tell Action Mailer not to deliver emails to the real world.
4448
# The :test delivery method accumulates sent emails in the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Users::ConfirmationsController, type: :controller do
4+
it "checks user is confirmed with token received by email" do
5+
user = FactoryBot.create(:user)
6+
7+
received_token = Devise.mailer.deliveries.last.body.match(/confirmation_token=(.+)">/x)[1]
8+
user = User.confirm_by_token(received_token)
9+
10+
expect(user.reload.confirmed?).to be(true)
11+
end
12+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Users::RegistrationsController, type: :controller do
4+
describe "after registration" do
5+
it "user is not confirmed" do
6+
user = FactoryBot.create :user
7+
expect(user.confirmed?).to_not be true
8+
end
9+
10+
it "sends welcome/confirmation email" do
11+
user = FactoryBot.build :user
12+
13+
expect { user.save }.to change(Devise.mailer.deliveries, :count).by(1)
14+
end
15+
16+
describe "check that email" do
17+
it "was sent to correct email" do
18+
user = FactoryBot.create :user
19+
20+
confirmation_email = Devise.mailer.deliveries.last
21+
expect(user.email).to eq confirmation_email.to[0]
22+
end
23+
24+
it "contains welcome message" do
25+
user = FactoryBot.create :user
26+
27+
confirmation_email = Devise.mailer.deliveries.last
28+
expect(confirmation_email.body.to_s).to have_content 'Welcome'
29+
end
30+
end
31+
end
32+
end

spec/factories/user.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FactoryBot.define do
2+
factory :user do
3+
email { FFaker::Internet.email }
4+
name { FFaker::Name.name }
5+
password { 'password' }
6+
end
7+
end

spec/models/.keep

Whitespace-only changes.

spec/models/user_spec.rb

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
require 'rails_helper'
22

33
RSpec.describe User, type: :model do
4-
subject {
5-
described_class.new(email: "john@doe.com",
6-
name: 'John',
7-
password: "john_password",
8-
)
9-
}
4+
user = FactoryBot.create(:user)
105

116
describe "Validations" do
127
it "should be valid" do
13-
expect(subject).to be_valid
8+
expect(user).to be_valid
149
end
1510

1611
it "should not be valid without an email" do
17-
subject.email = nil
18-
expect(subject).to_not be_valid
12+
user.email = nil
13+
expect(user).to_not be_valid
1914
end
2015

2116
it "should not be valid without a name" do
22-
subject.name = nil
23-
expect(subject).to_not be_valid
17+
user.name = nil
18+
expect(user).to_not be_valid
2419
end
2520

2621
it "name should be less than 32 characters" do
27-
subject.name = "this is a very very very long name"
28-
expect(subject).to_not be_valid
22+
user.name = "this is a very very very long name"
23+
expect(user).to_not be_valid
2924
end
3025

3126
it "should not be valid without a password" do
32-
subject.password = nil
33-
expect(subject).to_not be_valid
27+
user.password = nil
28+
expect(user).to_not be_valid
3429
end
3530

36-
it "password must be at least 6 characters" do
37-
subject.password = '12'
38-
expect(subject).to_not be_valid
31+
it "password must have at least 6 characters" do
32+
user.password = '12'
33+
expect(user).to_not be_valid
3934
end
4035
end
4136
end

spec/rails_helper.rb

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
require File.expand_path('../config/environment', __dir__)
77
# Prevent database truncation if the environment is production
88
abort('The Rails environment is running in production mode!') if Rails.env.production?
9+
10+
require 'factory_bot_rails'
911
require 'rspec/rails'
1012
# Add additional requires below this line. Rails is not loaded until this point!
1113

@@ -63,4 +65,8 @@
6365
config.filter_rails_from_backtrace!
6466
# arbitrary gems may also be filtered via:
6567
# config.filter_gems_from_backtrace("gem name")
68+
69+
config.before(:suite) do
70+
DatabaseCleaner.clean_with(:truncation)
71+
DatabaseCleaner.strategy = :transaction end
6672
end

spec/spec_helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#
1717
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
1818
RSpec.configure do |config|
19+
config.before(:suite) do
20+
DatabaseCleaner.clean_with(:truncation)
21+
end
22+
1923
# rspec-expectations config goes here. You can use an alternate
2024
# assertion/expectation library such as wrong or the stdlib/minitest
2125
# assertions if you prefer.

spec/support/factory_bot.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.configure do |config|
4+
config.include FactoryBot::Syntax::Methods
5+
end

0 commit comments

Comments
 (0)