From 4955481f14b99941708bfce1731901267389acfc Mon Sep 17 00:00:00 2001 From: Gopesh Tulsyan Date: Mon, 12 May 2014 14:19:46 +0530 Subject: [PATCH] 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