From 82c6c034c58e6a932033e845b49aa4820d5ae22e Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Sun, 16 Mar 2014 18:34:37 +0100 Subject: [PATCH] Improve test coverage of UsersController --- app/controllers/users_controller.rb | 2 +- spec/controllers/users_controller_spec.rb | 71 ++++++++++++++++++++++- spec/support/authentication.rb | 4 ++ 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0cee018..b2d8415 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -30,7 +30,7 @@ class UsersController < ApplicationController end def update - @user = User.find(current_user.id) + @user = current_user.clone if @user.update_attributes(update_params) redirect_to profile_path(@user), notice: 'Account settings saved.' diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 54432a5..d630940 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -97,7 +97,7 @@ describe UsersController do context "when real user username given" do let(:user) { create(:user) } - it 'renders "show" template with HomePagePresenter as page' do + it 'renders "show" template' do should render_template('show') end end @@ -120,11 +120,76 @@ describe UsersController do end describe '#edit' do - it 'should have specs' + subject { get :edit } + + let(:user) { create(:user) } + + before do + login_as(user) + end + + it "is successful" do + subject + expect(response.status).to eq(200) + end + + it 'renders "edit" template' do + subject + expect(response).to render_template(:edit) + end + + context "for guest user" do + before do + logout + end + + it "redirects to login page" do + subject + expect(response).to redirect_to(login_path) + end + end end describe '#update' do - it 'should have specs' + subject { put :update, user: { username: 'batman' } } + + let(:user) { create(:user) } + + before do + login_as(user) + end + + it "redirects to profile" do + subject + expect(response).to redirect_to(profile_path('batman')) + end + + context "when update fails" do + before do + allow(user).to receive(:update_attributes) { false } + end + + it "responds with 422 status code" do + subject + expect(response.status).to eq(422) + end + + it 'renders "edit" template' do + subject + expect(response).to render_template(:edit) + end + end + + context "for guest user" do + before do + logout + end + + it "redirects to login page" do + subject + expect(response).to redirect_to(login_path) + end + end end end diff --git a/spec/support/authentication.rb b/spec/support/authentication.rb index b8e31c8..2b626af 100644 --- a/spec/support/authentication.rb +++ b/spec/support/authentication.rb @@ -29,6 +29,10 @@ module Asciinema def login_as(user) controller.current_user = user end + + def logout + controller.current_user = nil + end end module FeatureHelpers