From eb44ad9079363356d2d68fb866ce1047164128c6 Mon Sep 17 00:00:00 2001 From: gabrielbaldao Date: Wed, 16 Oct 2019 11:11:09 -0300 Subject: [PATCH] start use ffaker and refact some tests --- rails/Gemfile | 4 +- rails/Gemfile.lock | 352 ------------------ rails/spec/factories/nursery_tables.rb | 4 +- .../restoration_activity_log_entries.rb | 4 + rails/spec/factories/users.rb | 4 +- rails/spec/factories/zones.rb | 2 +- rails/spec/models/dive_spec.rb | 47 +-- rails/spec/models/nursery_table_spec.rb | 27 +- rails/spec/models/zone_spec.rb | 6 +- rails/spec/rails_helper.rb | 4 +- 10 files changed, 46 insertions(+), 408 deletions(-) delete mode 100644 rails/Gemfile.lock diff --git a/rails/Gemfile b/rails/Gemfile index 5f823a55..43bc2969 100644 --- a/rails/Gemfile +++ b/rails/Gemfile @@ -51,6 +51,7 @@ group :development, :test do gem "byebug", platforms: %i[mri mingw x64_mingw] gem "dotenv-rails", "~> 2.7" gem "factory_bot_rails" + gem "ffaker", "~> 2.13.0" gem "pry" end @@ -66,7 +67,6 @@ end group :test do gem "capybara" gem "rspec-rails" - gem "selenium-webdriver" gem "teaspoon", github: "jejacks0n/teaspoon", branch: "master" gem "teaspoon-mocha" @@ -84,5 +84,3 @@ gem "jquery-rails" # get service-workers to play nicely with rails gem "rails-i18n" gem "serviceworker-rails", "~> 0.6" - -gem "faker", "~> 1.9" diff --git a/rails/Gemfile.lock b/rails/Gemfile.lock deleted file mode 100644 index f1f67ef4..00000000 --- a/rails/Gemfile.lock +++ /dev/null @@ -1,352 +0,0 @@ -GIT - remote: https://github.com/jejacks0n/teaspoon.git - revision: dbe293dfcf61c1756f61f8696e066c436f7dd57b - branch: master - specs: - teaspoon (1.1.5) - railties (>= 3.2.5) - teaspoon-mocha (2.3.3) - teaspoon (>= 1.0.0) - -GEM - remote: https://rubygems.org/ - specs: - actioncable (6.0.0) - actionpack (= 6.0.0) - nio4r (~> 2.0) - websocket-driver (>= 0.6.1) - actionmailbox (6.0.0) - actionpack (= 6.0.0) - activejob (= 6.0.0) - activerecord (= 6.0.0) - activestorage (= 6.0.0) - activesupport (= 6.0.0) - mail (>= 2.7.1) - actionmailer (6.0.0) - actionpack (= 6.0.0) - actionview (= 6.0.0) - activejob (= 6.0.0) - mail (~> 2.5, >= 2.5.4) - rails-dom-testing (~> 2.0) - actionpack (6.0.0) - actionview (= 6.0.0) - activesupport (= 6.0.0) - rack (~> 2.0) - rack-test (>= 0.6.3) - rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.0, >= 1.2.0) - actiontext (6.0.0) - actionpack (= 6.0.0) - activerecord (= 6.0.0) - activestorage (= 6.0.0) - activesupport (= 6.0.0) - nokogiri (>= 1.8.5) - actionview (6.0.0) - activesupport (= 6.0.0) - builder (~> 3.1) - erubi (~> 1.4) - rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.1, >= 1.2.0) - activejob (6.0.0) - activesupport (= 6.0.0) - globalid (>= 0.3.6) - activemodel (6.0.0) - activesupport (= 6.0.0) - activerecord (6.0.0) - activemodel (= 6.0.0) - activesupport (= 6.0.0) - activestorage (6.0.0) - actionpack (= 6.0.0) - activejob (= 6.0.0) - activerecord (= 6.0.0) - marcel (~> 0.3.1) - activesupport (6.0.0) - concurrent-ruby (~> 1.0, >= 1.0.2) - i18n (>= 0.7, < 2) - minitest (~> 5.1) - tzinfo (~> 1.1) - zeitwerk (~> 2.1, >= 2.1.8) - addressable (2.7.0) - public_suffix (>= 2.0.2, < 5.0) - ast (2.4.0) - autoprefixer-rails (9.6.5) - execjs - bcrypt (3.1.13) - bindex (0.8.1) - bootsnap (1.4.5) - msgpack (~> 1.0) - bootstrap (4.3.1) - autoprefixer-rails (>= 9.1.0) - popper_js (>= 1.14.3, < 2) - sassc-rails (>= 2.0.0) - builder (3.2.3) - byebug (11.0.1) - capybara (3.29.0) - addressable - mini_mime (>= 0.1.3) - nokogiri (~> 1.8) - rack (>= 1.6.0) - rack-test (>= 0.6.3) - regexp_parser (~> 1.5) - xpath (~> 3.2) - childprocess (3.0.0) - coderay (1.1.2) - coffee-rails (5.0.0) - coffee-script (>= 2.2.0) - railties (>= 5.2.0) - coffee-script (2.4.1) - coffee-script-source - execjs - coffee-script-source (1.12.2) - concurrent-ruby (1.1.5) - connection_pool (2.2.2) - crass (1.0.4) - devise (4.7.1) - bcrypt (~> 3.0) - orm_adapter (~> 0.1) - railties (>= 4.1.0) - responders - warden (~> 1.2.3) - devise-i18n (1.8.2) - devise (>= 4.6) - diff-lcs (1.3) - dotenv (2.7.5) - dotenv-rails (2.7.5) - dotenv (= 2.7.5) - railties (>= 3.2, < 6.1) - erubi (1.9.0) - execjs (2.7.0) - factory_bot (5.1.1) - activesupport (>= 4.2.0) - factory_bot_rails (5.1.1) - factory_bot (~> 5.1.0) - railties (>= 4.2.0) - faker (1.9.6) - i18n (>= 0.7) - ffi (1.11.1) - font-awesome-rails (4.7.0.5) - railties (>= 3.2, < 6.1) - globalid (0.4.2) - activesupport (>= 4.2.0) - i18n (1.7.0) - concurrent-ruby (~> 1.0) - jaro_winkler (1.5.3) - jbuilder (2.9.1) - activesupport (>= 4.2.0) - jquery-rails (4.3.5) - rails-dom-testing (>= 1, < 3) - railties (>= 4.2.0) - thor (>= 0.14, < 2.0) - listen (3.1.5) - rb-fsevent (~> 0.9, >= 0.9.4) - rb-inotify (~> 0.9, >= 0.9.7) - ruby_dep (~> 1.2) - loofah (2.3.0) - crass (~> 1.0.2) - nokogiri (>= 1.5.9) - mail (2.7.1) - mini_mime (>= 0.1.1) - marcel (0.3.3) - mimemagic (~> 0.3.2) - method_source (0.9.2) - mimemagic (0.3.3) - mini_mime (1.0.2) - mini_portile2 (2.4.0) - minitest (5.12.2) - msgpack (1.3.1) - nio4r (2.5.2) - nokogiri (1.10.4) - mini_portile2 (~> 2.4.0) - orm_adapter (0.5.0) - parallel (1.18.0) - parser (2.6.5.0) - ast (~> 2.4.0) - pg (1.1.4) - popper_js (1.14.5) - pry (0.12.2) - coderay (~> 1.1.0) - method_source (~> 0.9.0) - public_suffix (4.0.1) - puma (3.12.1) - rack (2.0.7) - rack-protection (2.0.7) - rack - rack-proxy (0.6.5) - rack - rack-test (1.1.0) - rack (>= 1.0, < 3) - rails (6.0.0) - actioncable (= 6.0.0) - actionmailbox (= 6.0.0) - actionmailer (= 6.0.0) - actionpack (= 6.0.0) - actiontext (= 6.0.0) - actionview (= 6.0.0) - activejob (= 6.0.0) - activemodel (= 6.0.0) - activerecord (= 6.0.0) - activestorage (= 6.0.0) - activesupport (= 6.0.0) - bundler (>= 1.3.0) - railties (= 6.0.0) - sprockets-rails (>= 2.0.0) - rails-dom-testing (2.0.3) - activesupport (>= 4.2.0) - nokogiri (>= 1.6) - rails-html-sanitizer (1.3.0) - loofah (~> 2.3) - rails-i18n (6.0.0) - i18n (>= 0.7, < 2) - railties (>= 6.0.0, < 7) - railties (6.0.0) - actionpack (= 6.0.0) - activesupport (= 6.0.0) - method_source - rake (>= 0.8.7) - thor (>= 0.20.3, < 2.0) - rainbow (3.0.0) - rake (13.0.0) - rb-fsevent (0.10.3) - rb-inotify (0.10.0) - ffi (~> 1.0) - redis (4.1.3) - regexp_parser (1.6.0) - responders (3.0.0) - actionpack (>= 5.0) - railties (>= 5.0) - rspec-core (3.9.0) - rspec-support (~> 3.9.0) - rspec-expectations (3.9.0) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.9.0) - rspec-mocks (3.9.0) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.9.0) - rspec-rails (3.9.0) - actionpack (>= 3.0) - activesupport (>= 3.0) - railties (>= 3.0) - rspec-core (~> 3.9.0) - rspec-expectations (~> 3.9.0) - rspec-mocks (~> 3.9.0) - rspec-support (~> 3.9.0) - rspec-support (3.9.0) - rubocop (0.75.1) - jaro_winkler (~> 1.5.1) - parallel (~> 1.10) - parser (>= 2.6) - rainbow (>= 2.2.2, < 4.0) - ruby-progressbar (~> 1.7) - unicode-display_width (>= 1.4.0, < 1.7) - rubocop-rails (2.3.2) - rack (>= 1.1) - rubocop (>= 0.72.0) - ruby-progressbar (1.10.1) - ruby_dep (1.5.0) - rubyzip (2.0.0) - sassc (2.2.1) - ffi (~> 1.9) - sassc-rails (2.1.2) - railties (>= 4.0.0) - sassc (>= 2.0) - sprockets (> 3.0) - sprockets-rails - tilt - selenium-webdriver (3.142.6) - childprocess (>= 0.5, < 4.0) - rubyzip (>= 1.2.2) - serviceworker-rails (0.6.0) - railties (>= 3.1) - sidekiq (6.0.2) - connection_pool (>= 2.2.2) - rack (>= 2.0.0) - rack-protection (>= 2.0.0) - redis (>= 4.1.0) - simple_form (5.0.1) - actionpack (>= 5.0) - activemodel (>= 5.0) - spring (2.1.0) - spring-watcher-listen (2.0.1) - listen (>= 2.7, < 4.0) - spring (>= 1.2, < 3.0) - sprockets (4.0.0) - concurrent-ruby (~> 1.0) - rack (> 1, < 3) - sprockets-rails (3.2.1) - actionpack (>= 4.0) - activesupport (>= 4.0) - sprockets (>= 3.0.0) - thor (0.20.3) - thread_safe (0.3.6) - tilt (2.0.10) - turbolinks (5.2.1) - turbolinks-source (~> 5.2) - turbolinks-source (5.2.0) - tzinfo (1.2.5) - thread_safe (~> 0.1) - uglifier (4.2.0) - execjs (>= 0.3.0, < 3) - unicode-display_width (1.6.0) - warden (1.2.8) - rack (>= 2.0.6) - web-console (4.0.1) - actionview (>= 6.0.0) - activemodel (>= 6.0.0) - bindex (>= 0.4.0) - railties (>= 6.0.0) - webpacker (4.0.7) - activesupport (>= 4.2) - rack-proxy (>= 0.6.1) - railties (>= 4.2) - websocket-driver (0.7.1) - websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.4) - xpath (3.2.0) - nokogiri (~> 1.8) - zeitwerk (2.2.0) - -PLATFORMS - ruby - -DEPENDENCIES - bootsnap (>= 1.1.0) - bootstrap (~> 4.3.1) - byebug - capybara - coffee-rails - devise (~> 4.6) - devise-i18n - dotenv-rails (~> 2.7) - factory_bot_rails - faker (~> 1.9) - font-awesome-rails - jbuilder (~> 2.5) - jquery-rails - listen (>= 3.0.5, < 3.2) - pg (>= 0.18, < 2.0) - pry - puma (~> 3.11) - rails (~> 6.0.0) - rails-i18n - rspec-rails - rubocop-rails - sassc-rails - selenium-webdriver - serviceworker-rails (~> 0.6) - sidekiq - simple_form - spring - spring-watcher-listen (~> 2.0.0) - sprockets-rails (>= 2.3.2) - teaspoon! - teaspoon-mocha - turbolinks (~> 5) - uglifier (>= 1.3.0) - web-console (>= 3.3.0) - webpacker - -RUBY VERSION - ruby 2.6.3p62 - -BUNDLED WITH - 2.0.1 diff --git a/rails/spec/factories/nursery_tables.rb b/rails/spec/factories/nursery_tables.rb index 74c26444..ecf8983b 100644 --- a/rails/spec/factories/nursery_tables.rb +++ b/rails/spec/factories/nursery_tables.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :nursery_table do - capacity { 12 } - name { "nursery" } + capacity { rand(1..500) } + name { FFaker::Name.name } zone_id { 1 } end end diff --git a/rails/spec/factories/restoration_activity_log_entries.rb b/rails/spec/factories/restoration_activity_log_entries.rb index 9667360a..70a569bb 100644 --- a/rails/spec/factories/restoration_activity_log_entries.rb +++ b/rails/spec/factories/restoration_activity_log_entries.rb @@ -1,4 +1,8 @@ FactoryBot.define do factory :restoration_activity_log_entry do + cleaned { FFaker::Boolean.random } + corals_added { rand(0..100) } + dead_corals { rand(0..100) } + bleached_corals { rand(0..100) } end end diff --git a/rails/spec/factories/users.rb b/rails/spec/factories/users.rb index 6eca6e17..d05e4221 100644 --- a/rails/spec/factories/users.rb +++ b/rails/spec/factories/users.rb @@ -1,6 +1,6 @@ FactoryBot.define do factory :user do - email { "example@email.com" } - password { "password" } + email { FFaker::Internet.email } + password { FFaker::Internet.password } end end diff --git a/rails/spec/factories/zones.rb b/rails/spec/factories/zones.rb index 92f47ea2..f5cd583e 100644 --- a/rails/spec/factories/zones.rb +++ b/rails/spec/factories/zones.rb @@ -1,5 +1,5 @@ FactoryBot.define do factory :zone do - name { "zone A" } + name { FFaker::Address.city } end end diff --git a/rails/spec/models/dive_spec.rb b/rails/spec/models/dive_spec.rb index 0c3ffd5d..8daf9ad6 100644 --- a/rails/spec/models/dive_spec.rb +++ b/rails/spec/models/dive_spec.rb @@ -2,44 +2,45 @@ RSpec.describe Dive do describe "validations" do - subject { FactoryBot.build(:dive) } + let(:dive) { build(:dive) } - it "has a valid state" do - expect(subject).to be_valid + describe "has a factory valid" do + it { expect(dive).to be_valid } end - it "requires the diver to have been responsible" do - subject.i_have_been_responsible = false - expect(subject).not_to be_valid + context "requires the diver to have been responsible" do + describe "responsible is false" do + let(:dive_not_responsible) { build :dive, i_have_been_responsible: false } - subject.i_have_been_responsible = nil - expect(subject).not_to be_valid + it { expect(subject).not_to be_valid } + end + describe "responsible is nil" do + let(:dive_not_responsible) { build :dive, i_have_been_responsible: nil } + + it { expect(subject).not_to be_valid } + end end end describe "finishing the dive" do - it "sets the finish time" do - subject = FactoryBot.create(:dive) - - subject.finish + let(:dive) { build :dive } + let!(:action) { dive.finish } - expect(subject.finished_at).to be_present - end + it { expect(dive.finished_at).to be_present } end - describe "checking if a dive is finished" do - it "is true if finished_at is set" do - subject = FactoryBot.build(:dive) - subject.finished_at = Time.zone.now + context "checking if a dive is finished" do + let(:dive) { create :dive } + describe "finished now" do + let!(:action) { dive.finished_at = Time.zone.now } - expect(subject).to be_finished + it { expect(dive).to be_finished } end - it "is false if finished_at is notset" do - subject = FactoryBot.build(:dive) - subject.finished_at = nil + describe "does not finished" do + let!(:action) { dive.finished_at = nil } - expect(subject).not_to be_finished + it { expect(dive).not_to be_finished } end end end diff --git a/rails/spec/models/nursery_table_spec.rb b/rails/spec/models/nursery_table_spec.rb index 7ba787a1..cde55f4c 100644 --- a/rails/spec/models/nursery_table_spec.rb +++ b/rails/spec/models/nursery_table_spec.rb @@ -1,27 +1,12 @@ require "rails_helper" RSpec.describe NurseryTable, type: :model do - let(:zone) { Zone.new(id: 1, name: "A") } - let(:nursery) { NurseryTable.new(capacity: 200, name: "A", zone_id: zone.id) } + let(:zone) { create :zone, id: 1 } + context "validates" do + describe "has a factory valid" do + subject(:nursery) { create :nursery_table, zone_id: zone.id } - it "has valid attributes" do - expect(nursery).to have_attributes(capacity: 200, name: "A", zone_id: zone.id) - end - - it "cannot have negative capacity" do - nursery = NurseryTable.new(capacity: -20, name: "A", zone_id: zone.id) - expect(nursery).to_not be_valid - end - - it "cannot be zero" do - zone = Zone.create(id: 13, name: "Add me") - nursery = NurseryTable.new(capacity: 0, name: "A", zone_id: zone.id) - expect(nursery).to_not be_valid - end - - it "atleast has one" do - zone = Zone.create(id: 23, name: "Add me") - nursery = NurseryTable.new(capacity: 1, name: "A", zone_id: zone.id) - expect(nursery).to be_valid + it { expect(nursery).to be_valid } + end end end diff --git a/rails/spec/models/zone_spec.rb b/rails/spec/models/zone_spec.rb index cbd50cce..db03636a 100644 --- a/rails/spec/models/zone_spec.rb +++ b/rails/spec/models/zone_spec.rb @@ -1,10 +1,10 @@ require "rails_helper" RSpec.describe Zone, type: :model do - let(:zone) { Zone.new(name: "A") } + let(:zone) { create :zone, name: "Fake name" } - it "is has valid attributes" do + it "has a valid factory", :aggregate_failures do expect(zone).to be_valid - expect(zone).to have_attributes(name: "A") + expect(zone).to have_attributes(name: "Fake name") end end diff --git a/rails/spec/rails_helper.rb b/rails/spec/rails_helper.rb index 9f435361..b9873770 100644 --- a/rails/spec/rails_helper.rb +++ b/rails/spec/rails_helper.rb @@ -8,7 +8,9 @@ # Add additional requires below this line. Rails is not loaded until this point! require_relative "./support/capybara" - +RSpec.configure do |config| + config.include FactoryBot::Syntax::Methods +end # 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