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/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 177c4566..78093092 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -2,9 +2,7 @@ class Admin::UsersController < ApplicationController before_filter :verify_admin def index - @users = User.all(:joins => :person, - :order => "people.last_name ASC", - :select => "users.*, + @users = User.joins(:person).order("people.last_name ASC").select("users.*, people.last_name AS last_name, people.first_name AS first_name, people.public_name AS public_name, diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb new file mode 100644 index 00000000..81923e86 --- /dev/null +++ b/spec/controllers/admin/users_controller_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe Admin::UsersController do + before(:each) do + @user = create(:user) + @admin = create(:admin) + sign_in(@admin) + end + describe "GET '#index'" do + it 'populates an array of users' do + user1 = create(:user, email: "gopesh.7500@gmail.com") + user2 = create(:user, email: "gopesh@gmail.com") + get :index + expect(assigns(:users)).to match_array([@user,@admin,user1,user2]) + end + + it 'renders index template' do + get :index + expect(response).to render_template :index + end + end +end \ No newline at end of file diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 0334d49e..62679644 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -2,9 +2,16 @@ FactoryGirl.define do factory :user do - email 'example@example.com' + sequence(:email) { |n| 'name#{n}@example.com' } password 'changeme' password_confirmation 'changeme' confirmed_at Time.now end + factory :admin, class: User do + sequence(:email) { |n| 'gopesh#{n}@exampleco.in' } + password 'changeme' + password_confirmation 'changeme' + confirmed_at Time.now + after(:create) { |user| user.role_ids = [3] } + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0df96be7..78e8340c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,13 +4,13 @@ Coveralls.wear!('rails') ENV["RAILS_ENV"] ||= 'test' 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 +# if Rails.configuration.database_configuration['test']['database'] == ':memory:' +# load "#{Rails.root}/db/schema.rb" +# load "#{Rails.root}/db/seeds.rb" +# end require 'rspec/rails' - +ActiveRecord::Migration.maintain_test_schema! # 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 @@ -30,19 +30,25 @@ RSpec.configure do |config| # config.mock_with :rr # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures - config.fixture_path = "#{::Rails.root}/spec/fixtures" + # config.fixture_path = "#{::Rails.root}/spec/fixtures" # 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 # the seed, which is printed after each run. # --seed 1234 config.order = "random" + + # Enables devise sign_in function + config.include Devise::TestHelpers, type: :controller + # Includes support/login_macros for feature tests + config.include SignInMacros, type: :feature + # Include factory_girls syntax config.include FactoryGirl::Syntax::Methods diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb new file mode 100644 index 00000000..4df8a28c --- /dev/null +++ b/spec/support/database_cleaner.rb @@ -0,0 +1,21 @@ +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 \ No newline at end of file diff --git a/spec/support/factory_girl.rb b/spec/support/factory_girl.rb index 031f2a19..68b44ed6 100644 --- a/spec/support/factory_girl.rb +++ b/spec/support/factory_girl.rb @@ -1,7 +1,5 @@ RSpec.configure do |config| - config.before(:suite) do FactoryGirl.lint end - end diff --git a/spec/support/seeds.rb b/spec/support/seeds.rb index 41e58998..d8a687a0 100644 --- a/spec/support/seeds.rb +++ b/spec/support/seeds.rb @@ -1,7 +1,5 @@ RSpec.configure do |config| - - config.before(:suite) do +config.before(:suite) do load "#{Rails.root}/db/seeds.rb" end - end diff --git a/spec/support/sign_in_macros.rb b/spec/support/sign_in_macros.rb new file mode 100644 index 00000000..4326c66e --- /dev/null +++ b/spec/support/sign_in_macros.rb @@ -0,0 +1,9 @@ +module SignInMacros + def sign_in(user) + visit new_user_session_path + fill_in 'user_email', with:'user.email' + fill_in 'user_password', with: 'user.password' + click_button 'Sign in' + page.should have_content('Signed in successfully') + end +end