spec/controllers/controller_spec.rb in remember_me-0.1.1 vs spec/controllers/controller_spec.rb in remember_me-1.0.0
- old
+ new
@@ -20,50 +20,69 @@
def user_signed_in?
!!current_user
end
end
-class SessionsController < ApplicationController
- def create
- current_user = User.new
- remember_me(current_user) if remember_me?
- redirect_to '/'
- end
+describe ApplicationController, type: :controller do
+ controller do
+ def index
+ redirect_to '/' and return if user_signed_in?
+ end
- def destroy
- forget_me(current_user)
- self.current_user = nil
- redirect_to '/signin'
- end
-end
+ def create
+ current_user = User.new
+ remember_me(current_user) if remember_me?
+ redirect_to '/'
+ end
-class HomesController < ApplicationController
- before_filter :authenticate_user!
-
- def index
- render nothing: true
+ def destroy
+ forget_me(current_user)
+ self.current_user = nil
+ redirect_to '/signin'
+ end
end
-end
-describe SessionsController do
let(:remember_me) { true }
- let(:attrs) { { remember_me: remember_me } }
+ let(:user) { User.new }
+ describe 'GET #index' do
+ context 'remember' do
+ before do
+ expect(User).to receive(:where).and_return([user])
+ expect(user).to receive(:remember_expired?).and_return(false)
+ expect(controller).to receive_message_chain(:cookies, :signed, :[]).and_return([user.id, Digest::SHA1.hexdigest(user.id)])
+ get :index
+ end
+
+ it { expect(response).to redirect_to('/') }
+ end
+
+ context 'raise error' do
+ before do
+ request.env['HTTP_ACCEPT'] = 'anonymous/html'
+ expect(controller).to receive_message_chain(:cookies, :signed, :[]).and_raise(NameError)
+ get :index
+ end
+
+ it { expect(response).not_to redirect_to('/') }
+ end
+ end
+
describe 'POST #create' do
before do
- controller.stub_chain(:cookies, :signed, :[]=) { '' }
- post :create, attrs
+ expect(controller).to receive_message_chain(:cookies, :signed, :[]=).and_return('')
+ post :create, params: { remember_me: remember_me }
end
- it { response.status.should eq 302 }
+ it { expect(response).to redirect_to('/') }
end
describe 'GET #destroy' do
before do
- controller.stub(:current_user) { User.new }
- controller.stub_chain(:cookies, :delete) { nil }
- get :destroy
+ expect(controller).to receive(:current_user).and_return(User.new)
+ expect(controller).to receive_message_chain(:cookies, :delete).and_return(nil)
+ delete :destroy, params: {id: user.id}
end
- it { response.status.should eq 302 }
+ it { expect(response.status).to eq 302 }
end
end