From 4955481f14b99941708bfce1731901267389acfc Mon Sep 17 00:00:00 2001 From: Gopesh Tulsyan Date: Mon, 12 May 2014 14:19:46 +0530 Subject: [PATCH 1/3] Some tests for users controller, and fixed users#index --- Gemfile | 1 + Gemfile.lock | 2 ++ app/controllers/admin/users_controller.rb | 4 +-- .../admin/users_controller_spec.rb | 34 +++++++++++++++++++ spec/factories/users.rb | 2 +- spec/spec_helper.rb | 18 ++++++---- spec/support/database_cleaner.rb | 23 +++++++++++++ spec/support/sigin_macros.rb | 10 ++++++ spec/support/signin_macros.rb | 0 9 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 spec/controllers/admin/users_controller_spec.rb create mode 100644 spec/support/database_cleaner.rb create mode 100644 spec/support/sigin_macros.rb create mode 100644 spec/support/signin_macros.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/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..c171900d --- /dev/null +++ b/spec/controllers/admin/users_controller_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe Admin::UsersController do + shared_examples 'access as administration' do + describe "GET '#index'" do + it 'populates an array of users' do + user1,user2 = create(:user,email:"gopesh.7500@gmail.com"),create(:user,email:"gopesh@gmail.com") + get :index + expect(assigns(:users)).to match_array([user1,user2]) + end + + it 'renders index template' do + get :index + expect(response).to render_template :index + end + end + + describe "PATCH '#update'" do + it 'locates the user' do + patch :update, id: @user.id, :user => {email:@user.email} + expect(assigns(:user)).to eq(@user) + end + end + + describe 'admin access' do + before(:each) do + @user = create(:user) + @admin = create(:admin) + sign_in(@admin) + end + it_behaves_like 'access as administration' + end + end +end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 0334d49e..d7b844d8 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -2,7 +2,7 @@ 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0df96be7..cb9dff8b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,10 +4,10 @@ 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' @@ -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..b3e90837 --- /dev/null +++ b/spec/support/database_cleaner.rb @@ -0,0 +1,23 @@ +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/sigin_macros.rb b/spec/support/sigin_macros.rb new file mode 100644 index 00000000..cc4201ed --- /dev/null +++ b/spec/support/sigin_macros.rb @@ -0,0 +1,10 @@ + +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 diff --git a/spec/support/signin_macros.rb b/spec/support/signin_macros.rb new file mode 100644 index 00000000..e69de29b From c97affca35258fc66777f357f8dfceae469723bd Mon Sep 17 00:00:00 2001 From: Gopesh Tulsyan Date: Mon, 12 May 2014 17:53:11 +0530 Subject: [PATCH 2/3] Some basic tests for user controller index --- .../admin/users_controller_spec.rb | 47 ++++++++----------- spec/factories/users.rb | 7 +++ spec/spec_helper.rb | 4 +- spec/support/database_cleaner.rb | 2 - spec/support/factory_girl.rb | 2 - spec/support/seeds.rb | 4 +- .../{sigin_macros.rb => sign_in_macros.rb} | 3 +- spec/support/signin_macros.rb | 0 8 files changed, 31 insertions(+), 38 deletions(-) rename spec/support/{sigin_macros.rb => sign_in_macros.rb} (91%) delete mode 100644 spec/support/signin_macros.rb diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb index c171900d..cf4a334e 100644 --- a/spec/controllers/admin/users_controller_spec.rb +++ b/spec/controllers/admin/users_controller_spec.rb @@ -1,34 +1,27 @@ require 'spec_helper' describe Admin::UsersController do - shared_examples 'access as administration' do - describe "GET '#index'" do - it 'populates an array of users' do - user1,user2 = create(:user,email:"gopesh.7500@gmail.com"),create(:user,email:"gopesh@gmail.com") - get :index - expect(assigns(:users)).to match_array([user1,user2]) - end - - it 'renders index template' do - get :index - expect(response).to render_template :index - end + + 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 + # pp response.body + expect(assigns(:users)).to match_array([@user,@admin,user1,user2]) end - describe "PATCH '#update'" do - it 'locates the user' do - patch :update, id: @user.id, :user => {email:@user.email} - expect(assigns(:user)).to eq(@user) - end - end - - describe 'admin access' do - before(:each) do - @user = create(:user) - @admin = create(:admin) - sign_in(@admin) - end - it_behaves_like 'access as administration' + it 'renders index template' do + get :index + # pp response.body + expect(response).to render_template :index end end -end +end \ No newline at end of file diff --git a/spec/factories/users.rb b/spec/factories/users.rb index d7b844d8..2db303fc 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -7,4 +7,11 @@ FactoryGirl.define do 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 cb9dff8b..78e8340c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,7 +10,7 @@ require File.expand_path("../../config/environment", __FILE__) # 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 @@ -47,7 +47,7 @@ RSpec.configure do |config| config.include Devise::TestHelpers, type: :controller # Includes support/login_macros for feature tests - config.include SigninMacros, type: :feature + 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 index b3e90837..4df8a28c 100644 --- a/spec/support/database_cleaner.rb +++ b/spec/support/database_cleaner.rb @@ -1,5 +1,4 @@ RSpec.configure do |config| - config.before(:suite) do DatabaseCleaner.clean_with(:truncation) end @@ -19,5 +18,4 @@ RSpec.configure do |config| 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/sigin_macros.rb b/spec/support/sign_in_macros.rb similarity index 91% rename from spec/support/sigin_macros.rb rename to spec/support/sign_in_macros.rb index cc4201ed..4326c66e 100644 --- a/spec/support/sigin_macros.rb +++ b/spec/support/sign_in_macros.rb @@ -1,5 +1,4 @@ - -module SigninMacros +module SignInMacros def sign_in(user) visit new_user_session_path fill_in 'user_email', with:'user.email' diff --git a/spec/support/signin_macros.rb b/spec/support/signin_macros.rb deleted file mode 100644 index e69de29b..00000000 From d50fd3cc23f1b6010f3edd967a6795c67c9c9fd3 Mon Sep 17 00:00:00 2001 From: Gopesh Tulsyan Date: Mon, 12 May 2014 17:57:35 +0530 Subject: [PATCH 3/3] Fixed houndci violations --- spec/controllers/admin/users_controller_spec.rb | 9 ++------- spec/factories/users.rb | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb index cf4a334e..81923e86 100644 --- a/spec/controllers/admin/users_controller_spec.rb +++ b/spec/controllers/admin/users_controller_spec.rb @@ -1,26 +1,21 @@ 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") + user1 = create(:user, email: "gopesh.7500@gmail.com") + user2 = create(:user, email: "gopesh@gmail.com") get :index - # pp response.body expect(assigns(:users)).to match_array([@user,@admin,user1,user2]) end it 'renders index template' do get :index - # pp response.body expect(response).to render_template :index end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 2db303fc..62679644 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -2,13 +2,13 @@ FactoryGirl.define do factory :user do - sequence(:email) {|n|'name#{n}@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'} + sequence(:email) { |n| 'gopesh#{n}@exampleco.in' } password 'changeme' password_confirmation 'changeme' confirmed_at Time.now