From 97c6f28b03c710a0a1801db2efee87f298f90f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Sat, 14 Aug 2021 11:58:51 -0300 Subject: [PATCH] improve test setup with factory bot --- Gemfile | 4 +++- Gemfile.lock | 23 ++++++++------------ spec/factories/admin_users.rb | 26 ++++++++++++++++++++++ spec/factories/balances.rb | 30 ++++++++++++++++++++++++++ spec/factories/currencies.rb | 16 ++++++++++++++ spec/factories/fiat_balances.rb | 28 ++++++++++++++++++++++++ spec/factories/user_documents.rb | 26 ++++++++++++++++++++++ spec/factories/users.rb | 30 ++++++++++++++++++++++++++ spec/rails_helper.rb | 3 +++ spec/views/home/index.html.erb_spec.rb | 6 +++++- 10 files changed, 176 insertions(+), 16 deletions(-) create mode 100644 spec/factories/admin_users.rb create mode 100644 spec/factories/balances.rb create mode 100644 spec/factories/currencies.rb create mode 100644 spec/factories/fiat_balances.rb create mode 100644 spec/factories/user_documents.rb create mode 100644 spec/factories/users.rb diff --git a/Gemfile b/Gemfile index 4801de5..3e49b10 100644 --- a/Gemfile +++ b/Gemfile @@ -28,8 +28,10 @@ gem "pundit" group :development, :test do gem "dotenv-rails" gem "pry-byebug", platforms: [:mri, :mingw, :x64_mingw] - gem "capybara" + gem "rspec-rails" + gem "faker", "~> 2.18" + gem "factory_bot_rails", "~> 6.2" gem "rubocop-rails", require: false gem "rubocop-rspec", require: false diff --git a/Gemfile.lock b/Gemfile.lock index 38f8401..b8c4546 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,8 +60,6 @@ GEM minitest (>= 5.1) tzinfo (~> 2.0) zeitwerk (~> 2.3) - addressable (2.8.0) - public_suffix (>= 2.0.2, < 5.0) administrate (0.16.0) actionpack (>= 5.0) actionview (>= 5.0) @@ -85,14 +83,6 @@ GEM msgpack (~> 1.0) builder (3.2.4) byebug (11.1.3) - capybara (3.35.3) - addressable - 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) coderay (1.1.3) concurrent-ruby (1.1.9) crass (1.0.6) @@ -114,6 +104,13 @@ GEM enumerize (2.4.0) activesupport (>= 3.2) erubi (1.10.0) + factory_bot (6.2.0) + activesupport (>= 5.0.0) + factory_bot_rails (6.2.0) + factory_bot (~> 6.2.0) + railties (>= 5.0.0) + faker (2.18.0) + i18n (>= 1.6, < 2) ffi (1.15.3) globalid (0.5.2) activesupport (>= 5.0) @@ -182,7 +179,6 @@ GEM pry-byebug (3.9.0) byebug (~> 11.0) pry (~> 0.13.0) - public_suffix (4.0.6) puma (5.4.0) nio4r (~> 2.0) pundit (2.1.0) @@ -318,8 +314,6 @@ GEM websocket-driver (0.7.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) - xpath (3.2.0) - nokogiri (~> 1.8) zeitwerk (2.4.2) PLATFORMS @@ -330,11 +324,12 @@ DEPENDENCIES administrate-field-active_storage annotate bootsnap (>= 1.4.4) - capybara devise devise-i18n dotenv-rails enumerize + factory_bot_rails (~> 6.2) + faker (~> 2.18) graphql graphql_playground-rails image_processing (~> 1.12) diff --git a/spec/factories/admin_users.rb b/spec/factories/admin_users.rb new file mode 100644 index 0000000..10f906b --- /dev/null +++ b/spec/factories/admin_users.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: admin_users +# +# id :bigint not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# remember_created_at :datetime +# reset_password_sent_at :datetime +# reset_password_token :string +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_admin_users_on_email (email) UNIQUE +# index_admin_users_on_reset_password_token (reset_password_token) UNIQUE +# +FactoryBot.define do + factory :admin_user do + sequence(:email) { |n| "admin-#{n}@example.com" } + password { "password" } + end +end diff --git a/spec/factories/balances.rb b/spec/factories/balances.rb new file mode 100644 index 0000000..1aa9df3 --- /dev/null +++ b/spec/factories/balances.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: balances +# +# id :bigint not null, primary key +# amount :decimal(20, 10) default(0.0), not null +# created_at :datetime not null +# updated_at :datetime not null +# currency_id :bigint not null +# user_id :bigint not null +# +# Indexes +# +# index_balances_on_currency_id (currency_id) +# index_balances_on_user_id (user_id) +# +# Foreign Keys +# +# fk_rails_... (currency_id => currencies.id) +# fk_rails_... (user_id => users.id) +# +FactoryBot.define do + factory :balance do + association :user + association :currency + amount { (rand * (10000 - 0) + 0) } + end +end diff --git a/spec/factories/currencies.rb b/spec/factories/currencies.rb new file mode 100644 index 0000000..e294e90 --- /dev/null +++ b/spec/factories/currencies.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: currencies +# +# id :bigint not null, primary key +# name :string not null +# created_at :datetime not null +# updated_at :datetime not null +# +FactoryBot.define do + factory :currency do + name { "CAKE" } + end +end diff --git a/spec/factories/fiat_balances.rb b/spec/factories/fiat_balances.rb new file mode 100644 index 0000000..37ac2a4 --- /dev/null +++ b/spec/factories/fiat_balances.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: fiat_balances +# +# id :bigint not null, primary key +# amount_cents :integer default(0), not null +# amount_currency :string default("BRL"), not null +# created_at :datetime not null +# updated_at :datetime not null +# user_id :bigint not null +# +# Indexes +# +# index_fiat_balances_on_user_id (user_id) +# +# Foreign Keys +# +# fk_rails_... (user_id => users.id) +# +FactoryBot.define do + factory :fiat_balance do + association :user + amount_currency { "BRL" } + amount_cents { Faker::Number.number(digits: 10) } + end +end diff --git a/spec/factories/user_documents.rb b/spec/factories/user_documents.rb new file mode 100644 index 0000000..a12861e --- /dev/null +++ b/spec/factories/user_documents.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: user_documents +# +# id :bigint not null, primary key +# status :string not null +# created_at :datetime not null +# updated_at :datetime not null +# user_id :bigint not null +# +# Indexes +# +# index_user_documents_on_user_id (user_id) +# +# Foreign Keys +# +# fk_rails_... (user_id => users.id) +# +FactoryBot.define do + factory :user_document do + association :user + status { :pending_review } + end +end diff --git a/spec/factories/users.rb b/spec/factories/users.rb new file mode 100644 index 0000000..fda8d14 --- /dev/null +++ b/spec/factories/users.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: users +# +# id :bigint not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# first_name :string not null +# last_name :string not null +# remember_created_at :datetime +# reset_password_sent_at :datetime +# reset_password_token :string +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_users_on_email (email) UNIQUE +# index_users_on_reset_password_token (reset_password_token) UNIQUE +# +FactoryBot.define do + factory :user do + first_name { Faker::Name.first_name } + last_name { Faker::Name.last_name } + email { Faker::Internet.email } + password { "password" } + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 990e786..edf45e1 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -66,6 +66,9 @@ RSpec.configure do |config| config.filter_rails_from_backtrace! # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") + + # Add FactoryBot + config.include(FactoryBot::Syntax::Methods) end Shoulda::Matchers.configure do |config| diff --git a/spec/views/home/index.html.erb_spec.rb b/spec/views/home/index.html.erb_spec.rb index 5399492..c26ae9b 100644 --- a/spec/views/home/index.html.erb_spec.rb +++ b/spec/views/home/index.html.erb_spec.rb @@ -2,5 +2,9 @@ require "rails_helper" RSpec.describe("home/index.html.erb", type: :view) do - pending "add some examples to (or delete) #{__FILE__}" + it "render div with id root" do + render + + expect(rendered).to(eq("
")) + end end