From dfd488fa9646ca6a499870e1cecd99dcae8ea200 Mon Sep 17 00:00:00 2001 From: zmagod Date: Mon, 10 Jul 2017 14:38:18 +0200 Subject: [PATCH] prevents cleaning data after rspec --- features/support/env.rb | 2 +- spec/factories/projects.rb | 24 ++++ spec/factories/teams.rb | 8 ++ spec/models/project_spec.rb | 15 ++- spec/models/sample_type_spec.rb | 2 +- spec/models/user_spec.rb | 200 ++++++++++++++++---------------- spec/rails_helper.rb | 21 +++- spec/spec_helper.rb | 1 + 8 files changed, 167 insertions(+), 106 deletions(-) create mode 100644 spec/factories/projects.rb create mode 100644 spec/factories/teams.rb diff --git a/features/support/env.rb b/features/support/env.rb index ff69adcb4..2e99aeed9 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -42,7 +42,7 @@ ActionController::Base.allow_rescue = false # Remove/comment out the lines below if your app doesn't have a database. # For some databases (like MongoDB and CouchDB) you may need to use :truncation instead. begin - DatabaseCleaner.strategy = :transaction + DatabaseCleaner.strategy = :truncation rescue NameError raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it." end diff --git a/spec/factories/projects.rb b/spec/factories/projects.rb new file mode 100644 index 000000000..b37f4b679 --- /dev/null +++ b/spec/factories/projects.rb @@ -0,0 +1,24 @@ +FactoryGirl.define do + factory :project do + association :created_by, factory: :project_user + association :team, factory: :team + archived false + name 'My project' + visibility 'hidden' + end + + factory :project_user, class: 'User' do + full_name Faker::Name.name + initials 'AD' + email Faker::Internet.email + password 'asdf1243' + password_confirmation 'asdf1243' + end + + factory :project_team do + association :created_by, factory: :project_user + name 'My team' + description 'Lorem ipsum dolor sit amet, consectetuer adipiscing eli.' + space_taken 1048576 + end +end diff --git a/spec/factories/teams.rb b/spec/factories/teams.rb new file mode 100644 index 000000000..f534e4d90 --- /dev/null +++ b/spec/factories/teams.rb @@ -0,0 +1,8 @@ +FactoryGirl.define do + factory :team do + association :created_by, factory: :user + name 'My team' + description 'Lorem ipsum dolor sit amet, consectetuer adipiscing eli.' + space_taken 1048576 + end +end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 2b0ca94af..c0f9fe1e4 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -40,6 +40,8 @@ describe Project, type: :model do end describe 'Should be a valid object' do + let(:user) { create :user } + let(:team) { create :team, created_by: user } it { should validate_presence_of :visibility } it { should validate_presence_of :team } it do @@ -47,8 +49,17 @@ describe Project, type: :model do .is_at_least(Constants::NAME_MIN_LENGTH) .is_at_most(Constants::NAME_MAX_LENGTH) end - it do - should validate_uniqueness_of(:name).scoped_to(:team).case_insensitive + + it 'should have a unique name scoped to team' do + FactoryGirl.create :project, + created_by: user, + last_modified_by: user, + team: team + project_two = FactoryGirl.build :project, + created_by: user, + last_modified_by: user, + team: team + expect(project_two).to_not be_valid end end end diff --git a/spec/models/sample_type_spec.rb b/spec/models/sample_type_spec.rb index 0fd62535f..5200e2d8c 100644 --- a/spec/models/sample_type_spec.rb +++ b/spec/models/sample_type_spec.rb @@ -27,6 +27,6 @@ describe SampleType, type: :model do it do should validate_length_of(:name).is_at_most(Constants::NAME_MAX_LENGTH) end - it { should validate_uniqueness_of(:name).scoped_to(:team) } + it { should validate_uniqueness_of(:name).scoped_to(:team).case_insensitive } end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a19ddddf7..aa55b0a06 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -6,109 +6,109 @@ describe User, type: :model do end describe 'Database table' do - it { is_expected.to have_db_column :id } - it { is_expected.to have_db_column :full_name } - it { is_expected.to have_db_column :initials } - it { is_expected.to have_db_column :email } - it { is_expected.to have_db_column :encrypted_password } - it { is_expected.to have_db_column :reset_password_token } - it { is_expected.to have_db_column :reset_password_sent_at } - it { is_expected.to have_db_column :sign_in_count } - it { is_expected.to have_db_column :current_sign_in_at } - it { is_expected.to have_db_column :last_sign_in_at } - it { is_expected.to have_db_column :current_sign_in_ip } - it { is_expected.to have_db_column :last_sign_in_ip } - it { is_expected.to have_db_column :created_at } - it { is_expected.to have_db_column :updated_at } - it { is_expected.to have_db_column :avatar_file_name } - it { is_expected.to have_db_column :avatar_content_type } - it { is_expected.to have_db_column :avatar_file_size } - it { is_expected.to have_db_column :avatar_updated_at } - it { is_expected.to have_db_column :confirmation_token } - it { is_expected.to have_db_column :confirmed_at } - it { is_expected.to have_db_column :confirmation_sent_at } - it { is_expected.to have_db_column :unconfirmed_email } - it { is_expected.to have_db_column :time_zone } - it { is_expected.to have_db_column :invitation_token } - it { is_expected.to have_db_column :invitation_created_at } - it { is_expected.to have_db_column :invitation_sent_at } - it { is_expected.to have_db_column :invitation_accepted_at } - it { is_expected.to have_db_column :invitation_limit } - it { is_expected.to have_db_column :invited_by_id } - it { is_expected.to have_db_column :invited_by_type } - it { is_expected.to have_db_column :invitations_count } - it { is_expected.to have_db_column :tutorial_status } - it { is_expected.to have_db_column :assignments_notification } - it { is_expected.to have_db_column :recent_notification } - it { is_expected.to have_db_column :assignments_notification_email } - it { is_expected.to have_db_column :recent_notification_email } - it { is_expected.to have_db_column :current_team_id } - it { is_expected.to have_db_column :system_message_notification_email } - it { is_expected.to have_db_column :authentication_token } + it { should have_db_column :id } + it { should have_db_column :full_name } + it { should have_db_column :initials } + it { should have_db_column :email } + it { should have_db_column :encrypted_password } + it { should have_db_column :reset_password_token } + it { should have_db_column :reset_password_sent_at } + it { should have_db_column :sign_in_count } + it { should have_db_column :current_sign_in_at } + it { should have_db_column :last_sign_in_at } + it { should have_db_column :current_sign_in_ip } + it { should have_db_column :last_sign_in_ip } + it { should have_db_column :created_at } + it { should have_db_column :updated_at } + it { should have_db_column :avatar_file_name } + it { should have_db_column :avatar_content_type } + it { should have_db_column :avatar_file_size } + it { should have_db_column :avatar_updated_at } + it { should have_db_column :confirmation_token } + it { should have_db_column :confirmed_at } + it { should have_db_column :confirmation_sent_at } + it { should have_db_column :unconfirmed_email } + it { should have_db_column :time_zone } + it { should have_db_column :invitation_token } + it { should have_db_column :invitation_created_at } + it { should have_db_column :invitation_sent_at } + it { should have_db_column :invitation_accepted_at } + it { should have_db_column :invitation_limit } + it { should have_db_column :invited_by_id } + it { should have_db_column :invited_by_type } + it { should have_db_column :invitations_count } + it { should have_db_column :tutorial_status } + it { should have_db_column :assignments_notification } + it { should have_db_column :recent_notification } + it { should have_db_column :assignments_notification_email } + it { should have_db_column :recent_notification_email } + it { should have_db_column :current_team_id } + it { should have_db_column :system_message_notification_email } + it { should have_db_column :authentication_token } end describe 'Relations' do - it { is_expected.to have_many :user_teams } - it { is_expected.to have_many :teams } - it { is_expected.to have_many :user_projects } - it { is_expected.to have_many :projects } - it { is_expected.to have_many :user_my_modules } - it { is_expected.to have_many :comments } - it { is_expected.to have_many :activities } - it { is_expected.to have_many :results } - it { is_expected.to have_many :samples } - it { is_expected.to have_many :samples_tables } - it { is_expected.to have_many :repositories } - it { is_expected.to have_many :repository_table_states } - it { is_expected.to have_many :steps } - it { is_expected.to have_many :custom_fields } - it { is_expected.to have_many :reports } - it { is_expected.to have_many :created_assets } - it { is_expected.to have_many :modified_assets } - it { is_expected.to have_many :created_checklists } - it { is_expected.to have_many :modified_checklists } - it { is_expected.to have_many :created_checklist_items } - it { is_expected.to have_many :modified_checklist_items } - it { is_expected.to have_many :modified_comments } - it { is_expected.to have_many :modified_custom_fields } - it { is_expected.to have_many :created_my_module_groups } - it { is_expected.to have_many :created_my_module_tags } - it { is_expected.to have_many :created_my_modules } - it { is_expected.to have_many :modified_my_modules } - it { is_expected.to have_many :archived_my_modules } - it { is_expected.to have_many :restored_my_modules } - it { is_expected.to have_many :created_teams } - it { is_expected.to have_many :modified_teams } - it { is_expected.to have_many :created_projects } - it { is_expected.to have_many :modified_projects } - it { is_expected.to have_many :archived_projects } - it { is_expected.to have_many :restored_projects } - it { is_expected.to have_many :modified_reports } - it { is_expected.to have_many :modified_results } - it { is_expected.to have_many :archived_results } - it { is_expected.to have_many :restored_results } - it { is_expected.to have_many :created_sample_groups } - it { is_expected.to have_many :modified_sample_groups } - it { is_expected.to have_many :assigned_sample_my_modules } - it { is_expected.to have_many :created_sample_types } - it { is_expected.to have_many :modified_sample_types } - it { is_expected.to have_many :modified_samples } - it { is_expected.to have_many :modified_steps } - it { is_expected.to have_many :created_tables } - it { is_expected.to have_many :modified_tables } - it { is_expected.to have_many :created_tags } - it { is_expected.to have_many :tokens } - it { is_expected.to have_many :modified_tags } - it { is_expected.to have_many :assigned_user_my_modules } - it { is_expected.to have_many :assigned_user_teams } - it { is_expected.to have_many :assigned_user_projects } - it { is_expected.to have_many :added_protocols } - it { is_expected.to have_many :archived_protocols } - it { is_expected.to have_many :restored_protocols } - it { is_expected.to have_many :assigned_my_module_repository_rows } - it { is_expected.to have_many :user_notifications } - it { is_expected.to have_many :notifications } - it { is_expected.to have_many :zip_exports } + it { should have_many :user_teams } + it { should have_many :teams } + it { should have_many :user_projects } + it { should have_many :projects } + it { should have_many :user_my_modules } + it { should have_many :comments } + it { should have_many :activities } + it { should have_many :results } + it { should have_many :samples } + it { should have_many :samples_tables } + it { should have_many :repositories } + it { should have_many :repository_table_states } + it { should have_many :steps } + it { should have_many :custom_fields } + it { should have_many :reports } + it { should have_many :created_assets } + it { should have_many :modified_assets } + it { should have_many :created_checklists } + it { should have_many :modified_checklists } + it { should have_many :created_checklist_items } + it { should have_many :modified_checklist_items } + it { should have_many :modified_comments } + it { should have_many :modified_custom_fields } + it { should have_many :created_my_module_groups } + it { should have_many :created_my_module_tags } + it { should have_many :created_my_modules } + it { should have_many :modified_my_modules } + it { should have_many :archived_my_modules } + it { should have_many :restored_my_modules } + it { should have_many :created_teams } + it { should have_many :modified_teams } + it { should have_many :created_projects } + it { should have_many :modified_projects } + it { should have_many :archived_projects } + it { should have_many :restored_projects } + it { should have_many :modified_reports } + it { should have_many :modified_results } + it { should have_many :archived_results } + it { should have_many :restored_results } + it { should have_many :created_sample_groups } + it { should have_many :modified_sample_groups } + it { should have_many :assigned_sample_my_modules } + it { should have_many :created_sample_types } + it { should have_many :modified_sample_types } + it { should have_many :modified_samples } + it { should have_many :modified_steps } + it { should have_many :created_tables } + it { should have_many :modified_tables } + it { should have_many :created_tags } + it { should have_many :tokens } + it { should have_many :modified_tags } + it { should have_many :assigned_user_my_modules } + it { should have_many :assigned_user_teams } + it { should have_many :assigned_user_projects } + it { should have_many :added_protocols } + it { should have_many :archived_protocols } + it { should have_many :restored_protocols } + it { should have_many :assigned_my_module_repository_rows } + it { should have_many :user_notifications } + it { should have_many :notifications } + it { should have_many :zip_exports } end describe 'Should be a valid object' do diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 6c59bf3e2..32c2525a0 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -2,7 +2,7 @@ require 'spec_helper' require 'shoulda-matchers' require 'database_cleaner' -ENV['RAILS_ENV'] ||= 'test' +ENV['RAILS_ENV'] = 'test' require File.expand_path('../../config/environment', __FILE__) # Prevent database truncation if the environment is production abort('The Rails environment is running in production mode!') if Rails.env.production? @@ -35,10 +35,27 @@ RSpec.configure do |config| # 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 + # http://www.virtuouscode.com/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/ + config.use_transactional_fixtures = false config.before(:suite) do DatabaseCleaner.clean_with(:truncation) end + + config.before(:each) do + DatabaseCleaner.strategy = :transaction + end + + config.before(:each, js: true) do + DatabaseCleaner.strategy = :truncation + end + + config.before(:each) do + DatabaseCleaner.start + end + + config.after(:each) do + DatabaseCleaner.clean + end # 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`. diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ffc1ac102..2b2309f8c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,6 +15,7 @@ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration require 'capybara/rspec' require 'simplecov' +require 'faker' RSpec.configure do |config| # rspec-expectations config goes here. You can use an alternate