From 8aa68fb84a4359ab888a2b91e7f394359b82d2f9 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 15:55:17 +0200 Subject: [PATCH] Installs database cleaner - preparation for feature tests with capybara see: http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/ --- Gemfile | 1 + Gemfile.lock | 2 ++ app/models/user.rb | 12 +++++++----- spec/spec_helper.rb | 3 +-- spec/support/database_cleaner.rb | 22 ++++++++++++++++++++++ 5 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 spec/support/database_cleaner.rb diff --git a/Gemfile b/Gemfile index 2c8fd572..7847bfd9 100644 --- a/Gemfile +++ b/Gemfile @@ -76,6 +76,7 @@ group :development, :test do gem 'rspec', '>= 3.0.0.beta' gem 'rspec-rails', '>= 3.0.0.beta' gem 'capybara' + gem 'database_cleaner' end # FIXME: We should use http://weblog.rubyonrails.org/2012/3/21/strong-parameters/ diff --git a/Gemfile.lock b/Gemfile.lock index 58e6d2a8..1e787f6e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -81,6 +81,7 @@ GEM thor d3_rails (3.4.6) railties (>= 3.1.0) + database_cleaner (1.2.0) devise (3.2.4) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -305,6 +306,7 @@ DEPENDENCIES cocoon coveralls d3_rails + database_cleaner devise factory_girl_rails formtastic (~> 2.3.0.rc3) diff --git a/app/models/user.rb b/app/models/user.rb index d4a32cf7..b60a60af 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -14,7 +14,7 @@ class User < ActiveRecord::Base accepts_nested_attributes_for :person accepts_nested_attributes_for :roles - before_save :setup_role + before_create :setup_role before_create :create_person def role?(role) @@ -27,12 +27,14 @@ class User < ActiveRecord::Base end def setup_role - if self.id == 1 - self.role_ids = [3] + if User.count == 0 + admin = Role.where(name: 'Admin').first + self.role_ids = [admin.id] unless admin.nil? end - + if self.role_ids.empty? - self.role_ids = [1] + participant = Role.where(name: 'Participant').first + self.role_ids = [participant.id] unless participant.nil? end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 33a10083..4a3c03f8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,7 +6,6 @@ require File.expand_path('../../config/environment', __FILE__) if Rails.configuration.database_configuration['test']['database'] == ':memory:' load "#{Rails.root}/db/schema.rb" - load "#{Rails.root}/db/seeds.rb" end require 'rspec/rails' @@ -35,7 +34,7 @@ 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 + config.use_transactional_fixtures = false # Run specs in random order to surface order dependencies. If you find an # order dependency and want to debug it, you can fix the order by providing diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb new file mode 100644 index 00000000..1e0de70f --- /dev/null +++ b/spec/support/database_cleaner.rb @@ -0,0 +1,22 @@ +RSpec.configure do |config| + + 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 +end