Skip to content

Instantly share code, notes, and snippets.

@DiegoOrejuela
Last active March 2, 2021 02:21
Show Gist options
  • Save DiegoOrejuela/e497dacf3c04d8583c5276022c3e8963 to your computer and use it in GitHub Desktop.
Save DiegoOrejuela/e497dacf3c04d8583c5276022c3e8963 to your computer and use it in GitHub Desktop.
Validations fields Devise on Ruby on Rails with Rspec and Shoulda Matchers
# spec/support/shared_examples/models/devise_model_shared_example.rb
name_shared_example = 'devise_model_shared_example'
RSpec.shared_examples name_shared_example do
describe name_shared_example do
describe "fields" do
context "Database authenticatable" do
it { should have_db_column(:email).of_type(:string).with_options(null: false, default: '') }
it { should have_db_column(:encrypted_password).of_type(:string).with_options(null: false, default: '') }
end
context "Recoverable" do
it { should have_db_column(:reset_password_token).of_type(:string) }
it { should have_db_column(:reset_password_sent_at).of_type(:datetime) }
end
context "Rememberable" do
it { should have_db_column(:remember_created_at).of_type(:datetime) }
end
context "Trackable" do
it { should have_db_column(:sign_in_count).of_type(:integer).with_options(null: false, default: 0) }
it { should have_db_column(:current_sign_in_at).of_type(:datetime) }
it { should have_db_column(:last_sign_in_at).of_type(:datetime) }
it { should have_db_column(:current_sign_in_ip).of_type(:string) }
it { should have_db_column(:last_sign_in_ip).of_type(:string) }
end
context "Confirmable" do
it { should have_db_column(:confirmation_token).of_type(:string) }
it { should have_db_column(:confirmed_at).of_type(:datetime) }
it { should have_db_column(:confirmation_sent_at).of_type(:datetime) }
it { should have_db_column(:unconfirmed_email).of_type(:string) }
end
context "Lockable" do
it { should have_db_column(:failed_attempts).of_type(:integer).with_options(null: false, default: 0) }
it { should have_db_column(:unlock_token).of_type(:string) }
it { should have_db_column(:locked_at).of_type(:datetime) }
end
context "Rails standard" do
it { should have_db_column(:id).of_type(:integer).with_options(null: false) }
it { should have_db_column(:created_at).of_type(:datetime).with_options(null: false) }
it { should have_db_column(:updated_at).of_type(:datetime).with_options(null: false) }
end
end
end
end
...
group :test do
gem 'rspec-rails'
gem 'shoulda-matchers'
end
...
# spec/spec_helper.rb
...
# Config Shoulda Matchers
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
# Shared examples and Shared context
# read more:
# - https://relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples
# - https://cassidy.codes/blog/2019-09-28-rspec-shared-examples-contexts/
Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
...
# spec/models/user_spec.rb
require 'rails_helper'
RSpec.describe User, type: :model do
include_examples "devise_model_shared_example"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment