prevents cleaning data after rspec

This commit is contained in:
zmagod 2017-07-10 14:38:18 +02:00
parent 6636190442
commit dfd488fa96
8 changed files with 167 additions and 106 deletions

View file

@ -42,7 +42,7 @@ ActionController::Base.allow_rescue = false
# Remove/comment out the lines below if your app doesn't have a database. # 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. # For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
begin begin
DatabaseCleaner.strategy = :transaction DatabaseCleaner.strategy = :truncation
rescue NameError rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it." raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end end

View file

@ -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

8
spec/factories/teams.rb Normal file
View file

@ -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

View file

@ -40,6 +40,8 @@ describe Project, type: :model do
end end
describe 'Should be a valid object' do 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 :visibility }
it { should validate_presence_of :team } it { should validate_presence_of :team }
it do it do
@ -47,8 +49,17 @@ describe Project, type: :model do
.is_at_least(Constants::NAME_MIN_LENGTH) .is_at_least(Constants::NAME_MIN_LENGTH)
.is_at_most(Constants::NAME_MAX_LENGTH) .is_at_most(Constants::NAME_MAX_LENGTH)
end 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 end
end end

View file

@ -27,6 +27,6 @@ describe SampleType, type: :model do
it do it do
should validate_length_of(:name).is_at_most(Constants::NAME_MAX_LENGTH) should validate_length_of(:name).is_at_most(Constants::NAME_MAX_LENGTH)
end end
it { should validate_uniqueness_of(:name).scoped_to(:team) } it { should validate_uniqueness_of(:name).scoped_to(:team).case_insensitive }
end end
end end

View file

@ -6,109 +6,109 @@ describe User, type: :model do
end end
describe 'Database table' do describe 'Database table' do
it { is_expected.to have_db_column :id } it { should have_db_column :id }
it { is_expected.to have_db_column :full_name } it { should have_db_column :full_name }
it { is_expected.to have_db_column :initials } it { should have_db_column :initials }
it { is_expected.to have_db_column :email } it { should have_db_column :email }
it { is_expected.to have_db_column :encrypted_password } it { should have_db_column :encrypted_password }
it { is_expected.to have_db_column :reset_password_token } it { should have_db_column :reset_password_token }
it { is_expected.to have_db_column :reset_password_sent_at } it { should have_db_column :reset_password_sent_at }
it { is_expected.to have_db_column :sign_in_count } it { should have_db_column :sign_in_count }
it { is_expected.to have_db_column :current_sign_in_at } it { should have_db_column :current_sign_in_at }
it { is_expected.to have_db_column :last_sign_in_at } it { should have_db_column :last_sign_in_at }
it { is_expected.to have_db_column :current_sign_in_ip } it { should have_db_column :current_sign_in_ip }
it { is_expected.to have_db_column :last_sign_in_ip } it { should have_db_column :last_sign_in_ip }
it { is_expected.to have_db_column :created_at } it { should have_db_column :created_at }
it { is_expected.to have_db_column :updated_at } it { should have_db_column :updated_at }
it { is_expected.to have_db_column :avatar_file_name } it { should have_db_column :avatar_file_name }
it { is_expected.to have_db_column :avatar_content_type } it { should have_db_column :avatar_content_type }
it { is_expected.to have_db_column :avatar_file_size } it { should have_db_column :avatar_file_size }
it { is_expected.to have_db_column :avatar_updated_at } it { should have_db_column :avatar_updated_at }
it { is_expected.to have_db_column :confirmation_token } it { should have_db_column :confirmation_token }
it { is_expected.to have_db_column :confirmed_at } it { should have_db_column :confirmed_at }
it { is_expected.to have_db_column :confirmation_sent_at } it { should have_db_column :confirmation_sent_at }
it { is_expected.to have_db_column :unconfirmed_email } it { should have_db_column :unconfirmed_email }
it { is_expected.to have_db_column :time_zone } it { should have_db_column :time_zone }
it { is_expected.to have_db_column :invitation_token } it { should have_db_column :invitation_token }
it { is_expected.to have_db_column :invitation_created_at } it { should have_db_column :invitation_created_at }
it { is_expected.to have_db_column :invitation_sent_at } it { should have_db_column :invitation_sent_at }
it { is_expected.to have_db_column :invitation_accepted_at } it { should have_db_column :invitation_accepted_at }
it { is_expected.to have_db_column :invitation_limit } it { should have_db_column :invitation_limit }
it { is_expected.to have_db_column :invited_by_id } it { should have_db_column :invited_by_id }
it { is_expected.to have_db_column :invited_by_type } it { should have_db_column :invited_by_type }
it { is_expected.to have_db_column :invitations_count } it { should have_db_column :invitations_count }
it { is_expected.to have_db_column :tutorial_status } it { should have_db_column :tutorial_status }
it { is_expected.to have_db_column :assignments_notification } it { should have_db_column :assignments_notification }
it { is_expected.to have_db_column :recent_notification } it { should have_db_column :recent_notification }
it { is_expected.to have_db_column :assignments_notification_email } it { should have_db_column :assignments_notification_email }
it { is_expected.to have_db_column :recent_notification_email } it { should have_db_column :recent_notification_email }
it { is_expected.to have_db_column :current_team_id } it { should have_db_column :current_team_id }
it { is_expected.to have_db_column :system_message_notification_email } it { should have_db_column :system_message_notification_email }
it { is_expected.to have_db_column :authentication_token } it { should have_db_column :authentication_token }
end end
describe 'Relations' do describe 'Relations' do
it { is_expected.to have_many :user_teams } it { should have_many :user_teams }
it { is_expected.to have_many :teams } it { should have_many :teams }
it { is_expected.to have_many :user_projects } it { should have_many :user_projects }
it { is_expected.to have_many :projects } it { should have_many :projects }
it { is_expected.to have_many :user_my_modules } it { should have_many :user_my_modules }
it { is_expected.to have_many :comments } it { should have_many :comments }
it { is_expected.to have_many :activities } it { should have_many :activities }
it { is_expected.to have_many :results } it { should have_many :results }
it { is_expected.to have_many :samples } it { should have_many :samples }
it { is_expected.to have_many :samples_tables } it { should have_many :samples_tables }
it { is_expected.to have_many :repositories } it { should have_many :repositories }
it { is_expected.to have_many :repository_table_states } it { should have_many :repository_table_states }
it { is_expected.to have_many :steps } it { should have_many :steps }
it { is_expected.to have_many :custom_fields } it { should have_many :custom_fields }
it { is_expected.to have_many :reports } it { should have_many :reports }
it { is_expected.to have_many :created_assets } it { should have_many :created_assets }
it { is_expected.to have_many :modified_assets } it { should have_many :modified_assets }
it { is_expected.to have_many :created_checklists } it { should have_many :created_checklists }
it { is_expected.to have_many :modified_checklists } it { should have_many :modified_checklists }
it { is_expected.to have_many :created_checklist_items } it { should have_many :created_checklist_items }
it { is_expected.to have_many :modified_checklist_items } it { should have_many :modified_checklist_items }
it { is_expected.to have_many :modified_comments } it { should have_many :modified_comments }
it { is_expected.to have_many :modified_custom_fields } it { should have_many :modified_custom_fields }
it { is_expected.to have_many :created_my_module_groups } it { should have_many :created_my_module_groups }
it { is_expected.to have_many :created_my_module_tags } it { should have_many :created_my_module_tags }
it { is_expected.to have_many :created_my_modules } it { should have_many :created_my_modules }
it { is_expected.to have_many :modified_my_modules } it { should have_many :modified_my_modules }
it { is_expected.to have_many :archived_my_modules } it { should have_many :archived_my_modules }
it { is_expected.to have_many :restored_my_modules } it { should have_many :restored_my_modules }
it { is_expected.to have_many :created_teams } it { should have_many :created_teams }
it { is_expected.to have_many :modified_teams } it { should have_many :modified_teams }
it { is_expected.to have_many :created_projects } it { should have_many :created_projects }
it { is_expected.to have_many :modified_projects } it { should have_many :modified_projects }
it { is_expected.to have_many :archived_projects } it { should have_many :archived_projects }
it { is_expected.to have_many :restored_projects } it { should have_many :restored_projects }
it { is_expected.to have_many :modified_reports } it { should have_many :modified_reports }
it { is_expected.to have_many :modified_results } it { should have_many :modified_results }
it { is_expected.to have_many :archived_results } it { should have_many :archived_results }
it { is_expected.to have_many :restored_results } it { should have_many :restored_results }
it { is_expected.to have_many :created_sample_groups } it { should have_many :created_sample_groups }
it { is_expected.to have_many :modified_sample_groups } it { should have_many :modified_sample_groups }
it { is_expected.to have_many :assigned_sample_my_modules } it { should have_many :assigned_sample_my_modules }
it { is_expected.to have_many :created_sample_types } it { should have_many :created_sample_types }
it { is_expected.to have_many :modified_sample_types } it { should have_many :modified_sample_types }
it { is_expected.to have_many :modified_samples } it { should have_many :modified_samples }
it { is_expected.to have_many :modified_steps } it { should have_many :modified_steps }
it { is_expected.to have_many :created_tables } it { should have_many :created_tables }
it { is_expected.to have_many :modified_tables } it { should have_many :modified_tables }
it { is_expected.to have_many :created_tags } it { should have_many :created_tags }
it { is_expected.to have_many :tokens } it { should have_many :tokens }
it { is_expected.to have_many :modified_tags } it { should have_many :modified_tags }
it { is_expected.to have_many :assigned_user_my_modules } it { should have_many :assigned_user_my_modules }
it { is_expected.to have_many :assigned_user_teams } it { should have_many :assigned_user_teams }
it { is_expected.to have_many :assigned_user_projects } it { should have_many :assigned_user_projects }
it { is_expected.to have_many :added_protocols } it { should have_many :added_protocols }
it { is_expected.to have_many :archived_protocols } it { should have_many :archived_protocols }
it { is_expected.to have_many :restored_protocols } it { should have_many :restored_protocols }
it { is_expected.to have_many :assigned_my_module_repository_rows } it { should have_many :assigned_my_module_repository_rows }
it { is_expected.to have_many :user_notifications } it { should have_many :user_notifications }
it { is_expected.to have_many :notifications } it { should have_many :notifications }
it { is_expected.to have_many :zip_exports } it { should have_many :zip_exports }
end end
describe 'Should be a valid object' do describe 'Should be a valid object' do

View file

@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
require 'shoulda-matchers' require 'shoulda-matchers'
require 'database_cleaner' require 'database_cleaner'
ENV['RAILS_ENV'] ||= 'test' ENV['RAILS_ENV'] = 'test'
require File.expand_path('../../config/environment', __FILE__) require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production # Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.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 # 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 # examples within a transaction, remove the following line or assign false
# instead of true. # 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 config.before(:suite) do
DatabaseCleaner.clean_with(:truncation) DatabaseCleaner.clean_with(:truncation)
end 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 # RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and # based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`. # `post` in specs under `spec/controllers`.

View file

@ -15,6 +15,7 @@
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
require 'capybara/rspec' require 'capybara/rspec'
require 'simplecov' require 'simplecov'
require 'faker'
RSpec.configure do |config| RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate # rspec-expectations config goes here. You can use an alternate